【问题标题】:TensorFlow, Variable weights/layer1 already exists, disallowedTensorFlow,可变权重/layer1 已存在,不允许
【发布时间】:2017-12-26 14:54:10
【问题描述】:

我在使用 TensorFlow 时遇到了与变量重用问题有关的错误。我的代码如下:

INPUT_NODE = 3000
OUTPUT_NODE = 20
LAYER1_NODE = 500

def get_weight_variable(shape, regularizer):
    weights = tf.get_variable(
            "weights", shape,
            initializer = tf.truncated_normal_initializer(stddev=0.1))

    if regularizer != None:
        tf.add_to_collection('losses', regularizer(weights))
    return weights


def inference(input_tensor, regularizer):
    with tf.variable_scope('layer1'):
        weights = get_weight_variable(
                [INPUT_NODE, LAYER1_NODE], regularizer)
        biases = tf.get_variable(
                "biases",[LAYER1_NODE],
                initializer = tf.constant_initializer(0.0))
        layer1 = tf.nn.relu(tf.matmul(input_tensor,weights) + biases)

    with tf.variable_scope('layer2'):
        weights = get_weight_variable(
                [LAYER1_NODE, OUTPUT_NODE], regularizer)
        biases = tf.get_variable(
                "biases",[OUTPUT_NODE],
                initializer = tf.constant_initializer(0.0))
        layer2 = tf.matmul(layer1,weights) + biases

    return layer2

def train():
    x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input')
    y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input')

    regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)

    y = inference(x, regularizer)

    #with other codes follows#

def main(argv=None):
    train()

if __name__ == '__main__':
    tf.app.run()

当我尝试运行代码时,出现错误:

ValueError: Variable layer1/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

我在 Stack Overflow 上查看了其他答案。看来问题与使用有关

with tf.variable_scope():

或者可能是 TensorFlow 的版本?任何人都可以帮我解决这个问题吗?非常感谢!

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    如果您尝试部分调用inference #with other codes follows#,则需要附加参数reuse,如下所示:

    ....
    
    def inference(input_tensor, regularizer, reuse):
        with tf.variable_scope('layer1', reuse = reuse):
         ....
    
    def train():
        x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input')
        y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input')
    
        regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
    
        y = inference(x, regularizer, False)
    
        #with other codes follows#
    
        z = inference(x, None, True)
    
        ....
    

    第一次在范围内创建变量,下一次“重用”它。

    【讨论】:

    • 非常感谢先生!但实际上我在后面没有调用推理函数。此代码根据github.com/caicloud/tensorflow-tutorial/blob/master/… 上的教程稍作修改,您可以在其中查看源代码。
    • 如果您显示出现错误的代码,我们可以更具体地讨论。如果您尝试在没有设置重用 = True 的情况下在同一范围内使用相同名称两次获取变量,则会出现此错误。
    • 您说得对,先生。发生错误是因为在训练过程中重复调用此函数,尽管没有显式调用。您可以单击链接以显示与我相同的代码。再次感谢您指出问题。我应该弄清楚在第一次调用后如何将参数重用设置为 True。
    猜你喜欢
    • 2018-02-07
    • 2018-02-13
    • 1970-01-01
    • 2019-03-05
    • 2018-02-07
    • 1970-01-01
    • 2012-09-19
    • 2013-03-12
    • 1970-01-01
    相关资源
    最近更新 更多