ValueError: 变量 conv/W 已经存在,不允许 [英] ValueError: Variable conv/W already exists, disallowed

查看:36
本文介绍了ValueError: 变量 conv/W 已经存在,不允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 TensorFlow 时遇到了错误.我想用"conv_W[0]"来初始化"conv/W",它们的形状是[3,3,192,32].我的代码如下:

def 卷积(X,重用 = 重用):使用 tf.variable_scope(范围或'conv',重用=重用):W = tf.get_variable("W", shape=[3,3,192,32])----------------------------------------------------------------------使用 tf.Session() 作为 sess:sess.run(tf.global_variables_initializer())sess.run(tf.local_variables_initializer())conv_w = tf.get_variable('conv/W', initializer=tf.constant_initializer(conv_W[0]))

错误是"ValueError: Variable conv/W already exists, disallowed.Did you mean to setureuse=True in VarScope? 最初定义于:"

解决方案

这可能是因为你在每个 epoch 初始化变量,所以换句话说错误意味着:你想共享这个变量还是你想重新声明它?由于所需的行为不清楚(创建新变量还是重用现有变量?)TensorFlow 将失败.如果您确实想共享变量,您可以更改该行:

conv_w = tf.get_variable('conv/W', initializer=tf.constant_initializer(conv_W[0]),reuse=True)

否则设置reuse=False,这将解决您的问题.

有关如何共享/取消共享变量的更多信息,请参阅 Tensorflow 文档::>

I was using TensorFlow and encountered an error. I want to use "conv_W[0]" to initialize "conv/W", they have the same shape of [3,3,192,32]. My code is as follows:

def convolutional(X,reuse = reuse):
    with tf.variable_scope(scope or 'conv', reuse=reuse):
        W = tf.get_variable("W", shape=[3,3,192,32])  
----------------------------------------------------------------------

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    conv_w = tf.get_variable('conv/W', initializer=tf.constant_initializer(conv_W[0]))

The error is "ValueError: Variable conv/W already exists, disallowed.Did you mean to set reuse=True in VarScope? Originally defined at:"

解决方案

That's probably happening because you initalize the variable on every epoch, so in other words the error means: Do you want to share this variable or you want to redeclare it? Since the desired behavior is unclear (create new variables or reuse the existing ones?) TensorFlow will fail. If you do want to share the variable you can just change the line:

conv_w = tf.get_variable('conv/W', initializer=tf.constant_initializer(conv_W[0]),reuse=True)

Otherwise set reuse=False, and that will solve your problem.

For more information about how to share/unshare variables see the Tensorflow documentation:

这篇关于ValueError: 变量 conv/W 已经存在,不允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆