【问题标题】:Output shape of tf.nn.conv2d_transpose is entirely undefined even though only batch size unknowntf.nn.conv2d_transpose 的输出形状完全未定义,即使只有批量大小未知
【发布时间】:2017-03-30 09:39:04
【问题描述】:

我正在使用以下代码在 Tensorflow 中计算 2D 反卷积:

def deconv2d_strided(input_, filter_h, filter_w, channels_in, channels_out, output_shape,
                     pool_factor=2, name='deconv', reuse=False):
    with tf.variable_scope(name, reuse=reuse):
        w = tf.get_variable(...)
        b = tf.get_variable(...)
        minus_b = tf.subtract(input_, b)

        # dynamic shape of input_:
        in_shape = tf.shape(input_)
        out_shape = [in_shape[0], output_shape[0].value, output_shape[1].value, channels_out]
        out = tf.nn.conv2d_transpose(minus_b,
                                     filter=w,
                                     output_shape=tf.pack(out_shape),
                                     strides=[1, pool_factor, pool_factor, 1],
                                     padding='SAME')
        return out

我面临的问题是out 的形状看起来像shape=(?, ?, ?, ?),尽管我传递给函数的out_shape 是完全定义的,除了第一个维度(即批量大小)。即

out_shape = <class 'list'>: [<tf.Tensor 'decoder/conv_l4/strided_slice:0' shape=() dtype=int32>, 5, 15, 256]

如何让out 的形状看起来像(?, 5, 15, 256)

我希望尽可能完整地定义形状,因为它的输出将进入批量归一化层,为此,通道维度(这是最后一个)必须完全定义,否则 Tensorflow 会抱怨(参见tf.contrib.layers.python.layers.batch_norm)。所以,如果你有一个解决方案,我可以如何将具有未定义通道维度的张量传递到批处理规范化层,这对我也有用,尽管我更愿意把这件事做好并弄清楚为什么conv2d_transpose 的输出形状未定义。

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    这个话题已经在 TensorFlow Issues 中讨论过了,看来他们还是要修复它。

    这里有Issuethis可以解决你的问题。

    【讨论】:

    • 我从this github issue 得到的另一种可能的解决方法是使用out.set_shape([None] + out_shape[1:])
    猜你喜欢
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 2019-12-27
    相关资源
    最近更新 更多