TensorFlow,可变权重/层1已存在,不允许

TensorFlow,可变权重/层1已存在,不允许,tensorflow,Tensorflow,我在使用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_ini

我在使用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:
我检查了关于堆栈溢出的其他答案。这个问题似乎与使用计算机有关

with tf.variable_scope():

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

如果您尝试调用
推理
部分
,其他代码如下#
您需要额外的参数
重用
,如下所示:

....

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)

    ....

第一次在作用域中创建变量,下一次“重用”它。

如果您尝试调用
推断
部分
,其他代码如下#
您需要额外的参数
重用
,类似这样:

....

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)

    ....

第一次在作用域中创建变量,下一次“重用”它。

非常感谢您,先生!但实际上我在后面的部分没有调用推理函数。这段代码是从上的教程中稍加修改的,您可以在那里看到源代码。如果您显示出现错误的代码,我们可以更具体地讨论。如果您尝试在同一范围内以相同的名称获取变量两次,但未设置reuse=True,则可能会出现此错误。您是正确的,先生。发生此错误是因为此函数在培训过程中被重复调用,尽管没有显式调用。你可以点击链接来显示下面的代码,这些代码与我的代码相同。再次感谢您指出问题所在。第一次调用参数reuse后,我应该知道如何将其设置为True。非常感谢您,先生!但实际上我在后面的部分没有调用推理函数。这段代码是从上的教程中稍加修改的,您可以在那里看到源代码。如果您显示出现错误的代码,我们可以更具体地讨论。如果您尝试在同一范围内以相同的名称获取变量两次,但未设置reuse=True,则可能会出现此错误。您是正确的,先生。发生此错误是因为此函数在培训过程中被重复调用,尽管没有显式调用。你可以点击链接来显示下面的代码,这些代码与我的代码相同。再次感谢您指出问题所在。我应该弄清楚如何在第一次调用参数reuse后将其设置为True。