我建立我自己的CNN
卷积层和完全连接层 之间,我需要知道卷积层的输出的大小,即有没有办法自动获取特征映射的形状并用它在Tensorflow中构建图形?
width_feature map * height_feature map * number_feature map
这样我就可以知道加权这之间的形状两层,即,
[number_neuron_output of convolution layer , number_neuron_1st fully connect layer].
我想要做的是自动获取[width_feature地图,height_feature,地图* number_feature图] ,因此可以用它来建立convolutoin层之间的连接和完全连接层
我试着像
def add_convtofully_layer(self,size_out,data_in):
shape_in=tf.shape(data_in)#[batch,H,W,C]
data_re=tf.reshape(data_in,[-1,shape_in[1]*shape_in[2]*shape_in[3]])
W=self.weight_NN(shape_in[1]*shape_in[2]*shape_in[3],size_out)
B=self.bias_NN(size_out)
data_drop=tf.nn.dropout(data_re,self.drop)
result=tf.nn.relu(tf.matmul(data_drop,W)+B)
return result
def weight_NN(self,w_in,w_out):
W=tf.Variable(tf.truncated_normal([w_in,w_out],stddev=0.1),name='weight')
return W
def bias_NN(self,out):
B=tf.Variable(tf.constant(0.0,dtype=tf.float32,shape=[out]),name='bias')
return B
但只得到了消息
ValueError: initial_value must have a shape specified: Tensor("Fully_connect_layer1/truncated_normal:0", shape=(?, 150), dtype=float32)
有没有办法使用Tensorflow做到这一点的方法吗?或者唯一的方法是我需要先自己计算一下?
谢谢!