tensorflow的conv2d_transpose使用后反向求梯度时会报错维度不匹配


源代码
import tensorflow as tf
import numpy as np


def model(input_tensor):
    with tf.device("/gpu:0"):
        weights = []
        biases = []
        tensor = None
 
        conv_00_w = tf.get_variable("conv_00_w", [3, 3, 1, 64],
                                    initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / 9)))
        conv_00_b = tf.get_variable("conv_00_b", [64], initializer=tf.constant_initializer(0))
        weights.append(conv_00_w)
        biases.append(conv_00_b)
        low_tensor = tf.nn.relu(
            tf.nn.bias_add(tf.nn.conv2d(input_tensor, conv_00_w, strides=[1, 1, 1, 1], padding='SAME'), conv_00_b))

        for i in range(18):
            conv_w = tf.get_variable("conv_d_w" % (i + 1), [3, 3, 64, 64],
                                     initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / 9 / 64)))
            conv_b = tf.get_variable("conv_d_b" % (i + 1), [64], initializer=tf.constant_initializer(0))
            weights.append(conv_w)
            biases.append(conv_b)
            tensor = tf.nn.relu(
                tf.nn.bias_add(tf.nn.conv2d(low_tensor, conv_w, strides=[1, 1, 1, 1], padding='SAME'), conv_b))
 
        tensor = tf.concat([tensor, low_tensor],3)
        for j in range(2):
            deconv_w = tf.get_variable("deconv_d_w" % (j + 1), [2, 2, 128, 128],initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / 4 / 128)))
            deconv_b = tf.get_variable("deconv_d_b" % (j + 1), [128], initializer=tf.constant_initializer(0))
            weights.append(deconv_w)
            biases.append(deconv_b)
            deconv_tensor = tf.nn.relu(
                tf.nn.bias_add(tf.nn.conv2d_transpose(tensor, deconv_w, [64,48*(j+1),48*(j+1),128], [1, 2, 2, 1], 'SAME'), deconv_b))

        conv_w = tf.get_variable("conv_20_w", [3, 3, 128, 1],
                                 initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / 9 / 128)))
        conv_b = tf.get_variable("conv_20_b", [1], initializer=tf.constant_initializer(0))
        weights.append(conv_w)
        biases.append(conv_b)
        tensor = tf.nn.bias_add(tf.nn.conv2d(deconv_tensor, conv_w, strides=[1, 1, 1, 1], padding='SAME'), conv_b)
        weights = weights + biases


        return tensor, weights
if __name__ == '__main__':
    INPUT_SIZE = (24, 24)
    OUTPUT_SIZE = (96, 96)
    BATCH_SIZE = 64
    train_input = tf.placeholder(tf.float32, shape=(BATCH_SIZE, INPUT_SIZE[0], INPUT_SIZE[1], 1))
    train_gt = tf.placeholder(tf.float32, shape=(BATCH_SIZE, OUTPUT_SIZE[0], OUTPUT_SIZE[1], 1))

    shared_model = tf.make_template('shared_model', model)

    train_output, weights = shared_model(train_input)
    loss = tf.reduce_sum(tf.nn.l2_loss(tf.subtract(train_output,train_gt)))
 
    learning_rate =  0.001 
    params = tf.trainable_variables()
 
    grads = tf.gradients(loss, weights)
    print("finished")

Traceback (most recent call last):
  File "C:\Users\111\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 560, in merge_with
    new_dims.append(dim.merge_with(other[i]))
  File "C:\Users\111\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 135, in merge_with
    self.assert_is_compatible_with(other)
  File "C:\Users\111\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 108, in assert_is_compatible_with
    % (self, other))
ValueError: Dimensions 48 and 24 are not compatible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/Experiments/self-experiments/experiment1/MODEL.py", line 80, in <module>
    grads = tf.gradients(loss, weights)
  File "C:\Users\111\Anaconda3\lib\site-packages\tensorflow\python\ops\gradients_impl.py", line 580, in gradients
    in_grad.set_shape(t_in.get_shape())
  File "C:\Users\111\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 413, in set_shape
    self._shape = self._shape.merge_with(shape)
  File "C:\Users\111\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 564, in merge_with
    (self, other))
ValueError: Shapes (64, 48, 48, 128) and (64, 24, 24, 128) are not compatible


会报错
已邀请:

Hung

赞同来自:


tf.nn.conv2d_transpose使用作为upsampling,即将24X24X1上采样为96x96X1,然后反向求梯度是就报错维度不匹配

要回复问题请先登录注册

返回顶部