Tensorflow conv2d_transpose无效的参数错误

时间:2019-04-02 02:54:54

标签: python tensorflow deep-learning

当我使用tensorflow训练模型时,遇到了一个困惑的问题,出现了错误,例如:
Invalid argument: Conv2DSlowBackpropInput: input and out_backprop must have the same batch size 更令人困惑的是,培训可以运行一些步骤,即batch_size = 64,当培训达到101步时,培训停止并抛出此错误。

我想使用TensorFlow训练我的模型,我想先使用conv2d_transpose,以实现升采样,然后要使用conv2d,在每次conv2d_transpose之后,我在conv2d_transpose之后添加一个batch_normalization。

但是当我训练模型时,我遇到了一个困惑的问题,模型可以运行一些步骤,但是当模型达到一定步骤时,就会引发问题。

问题是这样的: Invalid argument: Conv2DSlowBackpropInput: input and out_backprop must have the same batch size, input batch: 64 outbackprop batch: 56 batch_dim: 0

我的batch_size是64。


h1 = deconv2d(
            input_=h0,
            output_shape=[
                batch_size,
                s_h8,
                s_w8,
                f_dim * 4
            ],
            name="deconv1")

h1 = tf.nn.relu(d_bn_1(h1))  

def deconv2d(input_, output_shape,
             k_h=5, k_w=5, d_h=2, d_w=2, stddev=0.02,
             name="deconv2d", with_w=False):
    with tf.variable_scope(name):

        # batch_size = tf.shape(input_)[0]
        #
        # stack_shape = tf.stack([batch_size, output_shape[1], output_shape[2], output_shape[3]])

        w = tf.get_variable('w', [k_h, k_w, output_shape[-1], input_.get_shape()[-1]],
                            initializer=tf.random_normal_initializer(stddev=stddev))


        try:
            deconv = tf.nn.conv2d_transpose(input_, w, output_shape=output_shape,
                                            strides=[1, d_h, d_w, 1], padding="SAME")

        # Support for verisons of TensorFlow before 0.7.0
        except AttributeError:
            deconv = tf.nn.deconv2d(input_, w, output_shape=output_shape,
                                    strides=[1, d_h, d_w, 1])

        biases = tf.get_variable('biases', [output_shape[-1]], initializer=tf.constant_initializer(0.0))
        deconv = tf.reshape(tf.nn.bias_add(deconv, biases), deconv.get_shape())
        print(name+" shape", deconv.get_shape())

        if with_w:
            return deconv, w, biases
        else:
            return deconv

我希望模型可以流畅地训练,希望其他人可以帮助我解决这个问题。我尝试了一些方法,但是没有用。 非常感谢!!!!

1 个答案:

答案 0 :(得分:0)

经过多次尝试,终于找到了错误所在!!!!
好吧,我们需要在conv2d_transpose之后使用tf.reshape(),但这还不够,我们还需要在conv2d之后使用tf.reshape()。
在conv2d_transpose和conv2d之后需要tf.reshape()的原因可能是Tensor的形状会在训练中丢失。
然后,问题解决了!!!