Tensorflow笔记1-weights already exists, disallowed. did you mean to set reuse=True..

完整报错信息:

weights already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? 

出错原因:

tensorflow中,同一个变量在不同地方都被使用了两次。

解决办法:

方法一:重用变量,即同一个变量被共用,任何使用此变量的位置,都可对其进行修改。

方法二:更改命名空间,即将变量名放置在不同的命名空间下,变量有自身的作用域。

 

解决此类问题的步骤:

        第一步:找到出错位置

        第二步:找到出错原因

 

详细过程:

方法一:

之所以会出现此类报错信息的主要原因是同一变量名使用了两次,在第二次使用的时候就会报错,tensorflow中默认一个变量名不能重复使用。如下函数:

函数conv_relu定义了一个卷积操作和 激活函数,并且定义了变量名字为weights,biases两个变量

def conv_relu(input, kernel_shape, bias_shape):
    # Create variable named "weights".
    weights = tf.get_variable("weights", kernel_shape,
        initializer=tf.random_normal_initializer())
    # Create variable named "biases".
    biases = tf.get_variable("biases", bias_shape,
        initializer=tf.constant_intializer(0.0))
    conv = tf.nn.conv2d(input, weights,
        strides=[1, 1, 1, 1], padding='SAME')
    return tf.nn.relu(conv + biases)

函数my_image_fileter定义了一个简单的网络结构图,用于对图片的操作

def my_image_filter(input_images):
    with tf.variable_scope("conv1"):
        # Variables created here will be named "conv1/weights", "conv1/biases".
        relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])
    with tf.variable_scope("conv2"):
        # Variables created here will be named "conv2/weights", "conv2/biases".
        return conv_relu(relu1, [5, 5, 32, 32], [32])

 此时调用my_image_filter执行对两张图片的操作,

result1 = my_image_filter(image1)
result2 = my_image_filter(image2)
# Raises ValueError(... conv1/weights already exists ...)

 此时使用同一个函数对图片进行操作,可以共享变量的使用,因此为了能够共享变量,必须将变量设置为reuse=True,除了一下方法,还有其他方法,可以看官方文档,或错误提示进行修改。

with tf.variable_scope("image_filters") as scope:
    result1 = my_image_filter(image1)
    scope.reuse_variables()
    result2 = my_image_filter(image2)

 

 

方法二:方法一中的修改办法只适用于共享变量,如果变量名字相同,但是使用不同的变量值,两者互不影响,因此可以定义命名空间。 

for(int i=0;i<10;i++);
for(int i=0;i<5;i++);
//此两个for循环中的i值名字相同,但作用在不同的命名空间中,因此i值得改变互不影响。

本人当时得程序出错位置:

depthwise = depthwise_conv2d('depthwise', x=padded, w=None,kernel_size=(3, 1), stride=stride, l2_strength=l2_strength,
                                     padding='VALID', bias=bias, dilation_factor= dilation,
                                     activation=None, batchnorm_enabled=batchnorm_enabled, is_training=is_training)
depthwise = depthwise_conv2d('depthwise', x=depthwise, w=None,kernel_size=(1, 3), stride=stride, l2_strength=l2_strength,
                                     padding='VALID', bias=bias, dilation_factor= dilation,
                                     activation=None, batchnorm_enabled=batchnorm_enabled, is_training=is_training)

由于命名空间名字都为depthwise,因此,weights在使用时,出现了相同变量在不同位置调用了两次,但我们的目的是不能使用相同的变量,为了解决此类问题,可以将变量名处在不同的命名空间中。

depthwise = depthwise_conv2d('depthwise', x=padded, w=None,kernel_size=(3, 1), stride=stride, l2_strength=l2_strength,
                                     padding='VALID', bias=bias, dilation_factor= dilation,
                                     activation=None, batchnorm_enabled=batchnorm_enabled, is_training=is_training)
depthwise = depthwise_conv2d('xxxxxxxx', x=depthwise, w=None,kernel_size=(1, 3), stride=stride, l2_strength=l2_strength,
                                     padding='VALID', bias=bias, dilation_factor= dilation,
                                     activation=None, batchnorm_enabled=batchnorm_enabled, is_training=is_training)

这样权重weights名字相同,但一个是在命名空间depthwise中,一个在命名空间xxxxxxxx中,更改任意的weights,并不影响其他的weights值。

 

可以参考共享变量的理解:http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/variable_scope.html

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值