【问题标题】:input channels does not match filter's input channels (Tensorflow)输入通道与过滤器的输入通道不匹配(Tensorflow)
【发布时间】:2018-10-29 16:55:32
【问题描述】:

我想使用tf.nn.conv2d_transpose 为 GAN 网络构建反卷积层。

我想创建一个函数deconv_layer。它生成一个新层,该层输出filter_num 过滤器,expand_size 是输入分辨率的倍。

我的代码是:

def deconv_layer(x, filter_num, kernel_size=5, expand_size=2):

    x_shape = x.get_shape().as_list()

    with tf.name_scope('deconv_'+str(filter_num)):

        size_in = x_shape[-1]
        size_out = filter_num

        w = tf.Variable(tf.random_normal([kernel_size, kernel_size, size_in, size_out], mean=0.0, stddev=0.125), name="W")
        b = tf.Variable(tf.random_normal([size_out], mean=0.0, stddev=0.125), name="B")

        conv = tf.nn.conv2d_transpose(x, w, output_shape=[-1, x_shape[-3]*expand_size, x_shape[-2]*expand_size, filter_num], strides=[1,expand_size,expand_size,1], padding="SAME")
        act = tf.nn.relu(tf.nn.bias_add(conv, b))

        tf.summary.histogram('weights', w)
        tf.summary.histogram('biases', b)
        tf.summary.histogram('activations', act)

    return act

错误信息:

ValueError: input channels does not match filter's input channels
At conv = tf.nn.conv2d_transpose(...)

我不确定我是否正确使用了tf.nn.conv2d_transpose。我尝试基于卷积层创建它。

【问题讨论】:

    标签: python tensorflow neural-network conv-neural-network deconvolution


    【解决方案1】:

    过滤器尺寸错误。根据docs

    filter:一个 4-D Tensor,与 value 和 shape [height, 宽度,输出通道,输入通道]。过滤器的 in_channels 维度 必须与值(输入)匹配。

    您需要将w 的大小更改为:

    w = tf.Variable(tf.random_normal([kernel_size, kernel_size, size_out, size_in], mean=0.0, stddev=0.125), name="W")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-09
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多