【问题标题】:ValueError: Variable conv/W already exists, disallowedValueError: 变量 conv/W 已经存在,不允许
【发布时间】:2019-03-05 01:53:44
【问题描述】:

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

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]))

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

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-02-07
      • 1970-01-01
      • 2018-03-19
      • 2018-02-13
      • 1970-01-01
      • 2018-05-22
      • 2018-01-15
      • 2017-12-26
      • 2018-10-17
      相关资源
      最近更新 更多