如何正确组织Tensorflow模型层?

大家好!我们正在编写自己的AI,并且在创建正确的模型层时遇到了困难。我们需要输入到神经网络中的是一个list,它包含n个lists,每个lists中包含m个tuples

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

我们期望得到的结果是0或1(这确实有意义,请相信我)

这是我们目前的模型:

tpl = 3 # 因为我们有tuplesnl = 2 # 我们拥有的列表数量model = tf.keras.Sequential([# 这应该是能够理解我们列表的入口层            tf.keras.layers.Dense(nl * tpl , input_shape=(nl, tpl), activation='relu'),#隐藏层..            tf.keras.layers.Dense(64, input_shape=(nl, tpl), activation='sigmoid'),#我们的输出层有2个节点,一个应该包含0,另一个包含1,因为我们有两个标签(0和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       =================================================================

最后,

我们理解到我们提供的数据与最后一层不兼容,那么我们如何将最后一层转换为=>形状(None, 2),或者如何正确解决这个错误?


回答:

你可以在输出层之前使用Flatten()GlobalAveragePooling1D。完整示例:

import numpyimport tensorflow as tflist = numpy.array([[[1., 2., 4.], [5., 6., 8.]], [[5., 6., 0.], [7., 2., 4.]]])tpl = 3  nl = 2   model = tf.keras.Sequential([    tf.keras.layers.Dense(nl * tpl, input_shape=(nl, tpl), activation='relu'),    tf.keras.layers.Dense(64, input_shape=(nl, tpl), activation='sigmoid'),    tf.keras.layers.GlobalAveragePooling1D(),    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],       [0.41397247, 0.58602756]], dtype=float32)>

不过,你不会只得到0和1,你会得到每个类别的概率。另外,你应该避免使用内置关键字list

Model: "sequential_4"_________________________________________________________________Layer (type)                 Output Shape              Param #   =================================================================dense_12 (Dense)             (None, 2, 6)              24        _________________________________________________________________dense_13 (Dense)             (None, 2, 64)             448       _________________________________________________________________global_average_pooling1d (Gl (None, 64)                0         _________________________________________________________________dense_14 (Dense)             (None, 2)                 130       =================================================================Total params: 602Trainable params: 602Non-trainable params: 0_________________________________________________________________

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注