python - 神经网络的多维输入

标签 python tensorflow machine-learning deep-learning neural-network

我有一个多层神经网络。我有维度 [batch_size, 7, 4] 神经网络的输入。当这个输入通过网络传递时,我观察到只有输入的第三个维度不断变化,也就是说,如果我的第一层有20个输出,那么第二层的输出就是 [batch_size, 7, 20] 。我需要多层后的最终结果具有 [batchsize, 16] 的形状。

我有以下问题:

  • 是否使用了其他两个维度?
  • 如果没有,我该如何修改我的网络以便使用所有三个维度?
  • 如何有效地删除一维以获得我想要的二维输出?

以下是我当前在 Tensorflow v1.14Python 3 中的实现:

out1 = tf.layers.dense(inputs=noisy_data, units=150, activation=tf.nn.tanh)  # Outputs [batch, 7, 150]
out2 = tf.layers.dense(inputs=out1, units=75, activation=tf.nn.tanh)  # Outputs [batch, 7, 75] 
out3 = tf.layers.dense(inputs=out2, units=32, activation=tf.nn.tanh)  # Outputs [batch, 7, 32]
out4 = tf.layers.dense(inputs=out3, units=16, activation=tf.nn.tanh)  # Outputs [batch, 7, 16]

感谢任何帮助。谢谢。

最佳答案

问题 1 的回答:第二维 (axis=1) 中的数据值没有被使用,因为如果您查看下面的代码片段的输出(假设batch_size=2):

>>> input1 = tf.placeholder(float, shape=[2,7,4])
>>> tf.layers.dense(inputs=input1, units=150, activation=tf.nn.tanh)
>>> graph = tf.get_default_graph()
>>> graph.get_collection('variables')
[<tf.Variable 'dense/kernel:0' shape=(4, 150) dtype=float32_ref>, <tf.Variable 'dense/bias:0' shape=(150,) dtype=float32_ref>]

您可以看到dense层忽略了第二维上的值。但是,尽管官方tensorflow docs,但第一维的值将被视为批处理的一部分。没有说明所需的输入形状。

问题 2 的回答:Reshape在将输入传递给第一个 dense 之前,使用以下代码行将输入 [batch_size, 7, 4] 转换为 [batch_size, 28] > 图层:

input1 = tf.reshape(input1, [-1, 7*4])

问题 3 的回答:如果您按上述方式 reshape 输入,则无需删除维度。

关于python - 神经网络的多维输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59534825/

相关文章:

python - 为什么在 tensorflow 中的 batch 维度使用 None?

python - TensorFlow concat 中的排名不匹配错误

machine-learning - 在 Keras 2.0 上使用合并层(lambda/函数)?

python - 使用 pymongo 查询空字段

python - 创建多个 PhantomJS 实例还是只创建一个?

tensorflow - Keras h5将于2019年在Tensorflow上服?

machine-learning - 为什么信息增益特征选择给出零分

python - 在 Pandas 中向先前的单元格值添加计数

python - Pandas 按日期索引求和和分组

python - 为什么我需要将我的输入数据 reshape 为 Conv2D 的额外维度?