简体   繁体   English

神经网络的多维输入

[英]Multi-dimension input to a neural network

I have a neural network with many layers.我有一个有很多层的神经网络。 I have the input to the neural network of dimension [batch_size, 7, 4] .我有维度[batch_size, 7, 4]神经网络的输入。 When this input is passed through the network, I observed that only the third dimension of the input keeps changing, that is if my first layer has 20 outputs, then the output of the second layer is [batch_size, 7, 20] .当这个输入通过网络时,我观察到只有输入的第三维在不断变化,即如果我的第一层有 20 个输出,那么第二层的输出是[batch_size, 7, 20] I need the end result after many layers to be of the shape [batchsize, 16] .我需要多层后的最终结果是[batchsize, 16]形状。

I have the following questions:我有以下问题:

  • Are the other two dimensions being used at all?是否正在使用其他两个维度?
  • If not, how can I modify my network so that all three dimensions are used?如果没有,我如何修改我的网络以便使用所有三个维度?
  • How do I drop one dimension meaningfully to get the 2-d output that I desire?如何有意义地降低一维以获得我想要的二维输出?

Following is my current implementation in Tensorflow v1.14 and Python 3 :以下是我目前在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]

Any help is appreciated.任何帮助表示赞赏。 Thanks.谢谢。

Answer to Question 1 : The data values in 2nd dimension ( axis=1 ) are not being used because if you look at the output of code snippet below (assuming batch_size=2 ):问题 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>]

you can see that the dense layer ignores values along 2nd dimension.您可以看到dense层忽略了沿 2 维的值。 However, the values along 1st dimension would be considered as it is a part of a batch though the offical tensorflow docs doesn't say anything about the required input shape.但是,尽管官方tensorflow 文档没有说明所需的输入形状,但会考虑沿第一维的值,因为它是批次的一部分。

Answer to Question 2 : Reshape the input [batch_size, 7, 4] to [batch_size, 28] by using the below line of code before passing the input to the first dense layer:问题 2 的答案:在将输入传递到第一个dense层之前,使用以下代码行将输入[batch_size, 7, 4] 重塑[batch_size, 28]

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

Answer to Question 3 : If you reshape the inputs as above, there is no need to drop a dimension.问题 3 的答案:如果您按照上述方式对输入进行整形,则无需删除维度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM