在下面的代码中,我有2个最大池和2个卷积层。在pooling_out2之后,我想添加一个完全连接的图层。 如果我提到
`W-input=tf.reshape(pooling_out2, [-1,FLAGS.image_size*FLAGS.image_size*32])`
它将获取image的初始值。让我说我从图像大小28开始。我应该给它什么命令重塑pooling_out2的维度?
`batch_size = 4
input = tf.Variable(tf.random_normal([batch_size,FLAGS.image_size,FLAGS.image_size,FLAGS.input_channel]))
filter = weight_variable([FLAGS.image_size,FLAGS.image_size,FLAGS.input_channel,FLAGS.filter_channel])
filter_2=
weight_variable([FLAGS.filter_size,FLAGS.filter_size,FLAGS.filter_channel,32])
def conv2d(inputs,filters):
return tf.nn.conv2d(inputs,filters,strides=[1,2,2,1],padding='SAME')
def max_pool(conv_out):
return tf.nn.max_pool(conv_out,ksize=[1,FLAGS.filter_size,FLAGS.filter_size,1],strides=[1,2,2,1],padding='SAME')
conv_out1 = conv2d(input,filter)
pooling_out1 = max_pool(conv_out1)
conv_out2 = conv2d(pooling_out1,filter_2)
pooling_out2 = max_pool(conv_out2)`
答案 0 :(得分:0)
您可以使用命令tf.shape
获得张量流张量的形状(作为张量流量张量)然后在第一个之后将尺寸相乘就足够了,如下所示:
last_shape = tf.shape(pooling_out2)
n_features = tf.reduce_prod(last_shape[1:])
new_shape = [last_shape[0], n_features]
W_input = tf.reshape(pooling_out2, new_shape)