多维输入到神经网络

时间:2019-12-30 17:47:21

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

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

我有以下问题:

  • 其他两个维度是否都在使用?
  • 如果没有,如何修改我的网络,以便使用所有三个维度?
  • 如何有效地降低一维以获得所需的二维输出?

以下是我目前在 Tensorflow v1.14 Python 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 个答案:

答案 0 :(得分:0)

问题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并没有说明所需的输入形状,但可以将沿1维的值视为批处理的一部分。

问题2的答案:使用下面的代码行将输入[batch_size, 7, 4] [batch_size, 28]dense,然后将输入传递给第一个{{1 }}层:

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

问题3的答案:如果您按上述方法重塑输入,则无需删除尺寸。

相关问题