[英]Multi-dimension input to a 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 的答案:未使用第二维 ( 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
层忽略了沿 2 维的值。 但是,尽管官方tensorflow 文档没有说明所需的输入形状,但会考虑沿第一维的值,因为它是批次的一部分。
问题 2 的答案:在将输入传递到第一个dense
层之前,使用以下代码行将输入[batch_size, 7, 4]
重塑为[batch_size, 28]
:
input1 = tf.reshape(input1, [-1, 7*4])
问题 3 的答案:如果您按照上述方式对输入进行整形,则无需删除维度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.