【发布时间】: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