【问题标题】:Is there a way to automatically get the shape of feature map and use it to build the graph in Tensorflow?有没有办法自动获取特征图的形状并使用它在 Tensorflow 中构建图形?
【发布时间】:2017-04-11 15:04:43
【问题描述】:

我正在建立自己的 CNN
卷积层和全连接层之间 我需要知道卷积层输出的大小,即

width_feature map * height_feature map * number_feature map

这样我就可以知道这两层之间的权重形状,也就是

[number_neuron_output of convolution layer , number_neuron_1st full connect layer].

我想要做的是得到 [width_feature map,height_feature,map*number_feature map] 自动,因此可以使用它来建立卷积层和全连接层之间的连接

我试过了

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 必须具有指定的形状:Tensor("Fully_connect_layer1/truncated_normal:0", shape=(?, 150), dtype=float32)

有没有办法使用 Tensorflow 来做到这一点?或者唯一的方法是我需要先自己计算?

谢谢!

【问题讨论】:

    标签: python tensorflow conv-neural-network


    【解决方案1】:

    看起来您的输入仍然具有可变的批次维度。 (shape=(?, 150) 中的 ? 告诉您这一点,因为 ? 代表可变大小)。不,你不能初始化一个可变大小的变量,因为 tensorflow 不能正确分配内存。

    虽然您在忽略shape_in 的第一个值时似乎去除了批处理维度,但有两种可能性:第一种来自您刚刚传递的size_out 参数。另一个是您不小心混淆了shape_in 中的尺寸,实际上并没有消除问题。

    我的建议是在调试器中运行代码 - 虽然您无法在 tensorflow 等符号库中跟踪值,但您可以轻松查看不同变量的形状。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      相关资源
      最近更新 更多