如何正确组织 Tensorflow 模型层?

世界您好!我们正在编写自己的 AI,并且努力创建正确的模型层。我们必须在我们的神经网络中输入的是一个list包含 nlists和 mtuples


e.x. list = numpy.array([ [[1,2,4],[5,6,8]] , [[5,6,0],[7,2,4]] ])

我们期望得到的结果是 0 或 1(相信我,这是有道理的)


这就是我们现在所拥有的:


tpl = 3 # because we have tuples

nl = 2 # number of lists we have

model = tf.keras.Sequential([

# this should be entry layer that understands our list

            tf.keras.layers.Dense(nl * tpl , input_shape=(nl, tpl), activation='relu'),


#hidden layers..

            tf.keras.layers.Dense(64, input_shape=(nl, tpl), activation='sigmoid'),


#our output layer with 2 nodes that one should contain 0, other 1, because we have 2 labels ( 0 and 1 )

            tf.keras.layers.Dense(2, input_shape=(0, 1), activation='softmax')

        ])

但是我们得到以下错误:


/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)

     58     ctx.ensure_initialized()

     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,

---> 60                                         inputs, attrs, num_outputs)

     61   except core._NotOkStatusException as e:

     62     if name is not None:


InvalidArgumentError:  Incompatible shapes: [56,2,2] vs. [56,1]

     [[node huber_loss/Sub (defined at <ipython-input-25-08eb2e0b395e>:53) ]] [Op:__inference_train_function_45699]


Function call stack:

train_function

如果我们总结我们的模型,它会给出以下结构:


Layer (type)                 Output Shape              Param #   

=================================================================

dense_1 (Dense)             (None, 2, 6)              24        

_________________________________________________________________

dense_2 (Dense)             (None, 2, 64)             448       

_________________________________________________________________

dense_3 (Dense)             (None, 2, 2)              130       

=================================================================

最后,


我们了解到,我们提供的数据与最后一层不兼容,那么我们如何将最后一层转换为 => shape (None, 2)或者解决此错误的正确方法是什么?


慕容森
浏览 108回答 1
1回答

森林海

您可以在输出层之前使用Flatten()或。GlobalAveragePooling1D完整示例:import numpyimport tensorflow as tflist = numpy.array([[[1., 2., 4.], [5., 6., 8.]], [[5., 6., 0.], [7., 2., 4.]]])tpl = 3&nbsp;&nbsp;nl = 2&nbsp; &nbsp;model = tf.keras.Sequential([&nbsp; &nbsp; tf.keras.layers.Dense(nl * tpl, input_shape=(nl, tpl), activation='relu'),&nbsp; &nbsp; tf.keras.layers.Dense(64, input_shape=(nl, tpl), activation='sigmoid'),&nbsp; &nbsp; tf.keras.layers.GlobalAveragePooling1D(),&nbsp; &nbsp; tf.keras.layers.Dense(2, input_shape=(0, 1), activation='softmax')])model.build(input_shape=(nl, tpl))model(list)<tf.Tensor: shape=(2, 2), dtype=float32, numpy=array([[0.41599566, 0.58400434],&nbsp; &nbsp; &nbsp; &nbsp;[0.41397247, 0.58602756]], dtype=float32)>你不会只得到 0 和 1,你会得到每个班级的概率。你也应该隐藏内置关键字list。Model: "sequential_4"_________________________________________________________________Layer (type)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Output Shape&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Param #&nbsp; &nbsp;=================================================================dense_12 (Dense)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(None, 2, 6)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 24&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;_________________________________________________________________dense_13 (Dense)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(None, 2, 64)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;448&nbsp; &nbsp; &nbsp; &nbsp;_________________________________________________________________global_average_pooling1d (Gl (None, 64)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_________________________________________________________________dense_14 (Dense)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(None, 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;130&nbsp; &nbsp; &nbsp; &nbsp;=================================================================Total params: 602Trainable params: 602Non-trainable params: 0_________________________________________________________________
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python