Tensorflow报错:ValueError: Trying to share variable ..., but specified shape ... and found shape ...

Tensorflow报错:ValueError: Trying to share variable CON/conv2/W, but specified shape (3, 3, 128, 256) and found shape (3, 3, 128, 128).

我的使用情景是这样的:

我在一个卷积层提供了一个filter,并且它的shape为(3, 3, 128, 256), 然后我通过tf.variable_scope等操作使这个filter的name为CON/conv2/W。在使用tf.get_variable生成初始变量时就报了这样一个bug。

部分代码流程:

...

conv3 = conv_layer_new(relu2, [3, 3, 128, 256], strides = [1, 2, 2, 1], name = 'conv2')

...





def conv_layer_new(input, filter_shape, strides, name = None):

    with tf.variable_scope(name):
        W = tf.get_variable("W", filter_shape, initializer = tf. truncated_normal_initializer(0., 0.005))

    ...

重点来了:

那为什么它会自动给我一个(3, 3, 128, 128)这样一个特定的shape呢?因为报错的原话是 found !

所以我就思考难道是之前有相同name的filter,它的shape就是(3, 3, 128, 128)?

结果证明我是对的,确实在这个出错的卷积层使用了与之前相的name,导致调用了之前的shape。我将conv2改为了conv3之后就正常了。(也就是name变为了CON/conv3/W

所以说博主写了这么多废话就是想告诉大家一定要检查一下前面的变量名,是否写重复了,因为如果有相同的name,tensorflow就会自作主张地进行调用!(博主比较傻,这个问题卡了我好久所以写篇博客怕自己下次再犯。。。)

猜你喜欢

转载自blog.csdn.net/yyhhlancelot/article/details/82979235