我尝试运行以下TensorFlow代码,第一次运行时一切正常。如果我再次尝试运行它,就会持续抛出错误,称
ValueError: Variable layer1/weights1 already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at: File "C:\Users\owner\Anaconda3\envs\DeepLearning_NoGPU\lib\site-packages\tensorflow\python\framework\ops.py", line 1228, in __init__ self._traceback = _extract_stack() File "C:\Users\owner\Anaconda3\envs\DeepLearning_NoGPU\lib\site-packages\tensorflow\python\framework\ops.py", line 2336, in create_op original_op=self._default_original_op, op_def=op_def) File "C:\Users\owner\Anaconda3\envs\DeepLearning_NoGPU\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op op_def=op_def)
如果我重启控制台然后再次运行,它又能正常运行了。
下面是我实现的神经网络代码。
import pandas as pdimport numpy as npfrom sklearn.preprocessing import StandardScalerimport tensorflow as tflearning_rate = 0.001training_epochs = 100n_input = 9n_output = 1n_layer1_node = 100n_layer2_node = 100X_train = np.random.rand(100, 9)y_train = np.random.rand(100, 1)with tf.variable_scope('input'): X = tf.placeholder(tf.float32, shape=(None, n_input))with tf.variable_scope('output'): y = tf.placeholder(tf.float32, shape=(None, 1))#layer 1with tf.variable_scope('layer1'): weight_matrix1 = {'weights': tf.get_variable(name='weights1', shape=[n_input, n_layer1_node], initializer=tf.contrib.layers.xavier_initializer()), 'biases': tf.get_variable(name='biases1', shape=[n_layer1_node], initializer=tf.zeros_initializer())} layer1_output = tf.nn.relu(tf.add(tf.matmul(X, weight_matrix1['weights']), weight_matrix1['biases']))#Layer 2with tf.variable_scope('layer2'): weight_matrix2 = {'weights': tf.get_variable(name='weights2', shape=[n_layer1_node, n_layer2_node], initializer=tf.contrib.layers.xavier_initializer()), 'biases': tf.get_variable(name='biases2', shape=[n_layer2_node], initializer=tf.zeros_initializer())} layer2_output = tf.nn.relu(tf.add(tf.matmul(layer1_output, weight_matrix2['weights']), weight_matrix2['biases']))#Output layerwith tf.variable_scope('layer3'): weight_matrix3 = {'weights': tf.get_variable(name='weights3', shape=[n_layer2_node, n_output], initializer=tf.contrib.layers.xavier_initializer()), 'biases': tf.get_variable(name='biases3', shape=[n_output], initializer=tf.zeros_initializer())} prediction = tf.nn.relu(tf.add(tf.matmul(layer2_output, weight_matrix3['weights']), weight_matrix3['biases']))cost = tf.reduce_mean(tf.squared_difference(prediction, y))optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)with tf.Session() as session: session.run(tf.global_variables_initializer()) for epoch in range(training_epochs): session.run(optimizer, feed_dict={X: X_train, y: y_train}) train_cost = session.run(cost, feed_dict={X: X_train, y:y_train}) print(epoch, " epoch(s) done") print("training complete")
正如错误提示所示,我尝试在with tf.variable_scope():
中添加reuse=True
作为参数,但这仍然不起作用。
我在conda环境中运行这个代码。我使用的是Python 3.5和CUDA 8(但这应该无关紧要,因为它没有配置为在GPU上运行),在Windows 10上运行。
回答:
这是TensorFlow工作方式的问题。需要理解的是,TensorFlow有一个“隐藏”的状态——正在构建的图。大多数tf函数在这个图中创建操作(如每个tf.Variable调用,每个算术运算等)。另一方面,实际的“执行”发生在tf.Session()中。因此,您的代码通常会看起来像这样:
build_graph()with tf.Session() as sess: process_something()
由于所有实际的变量、结果等都只存在于会话中,如果您想“运行两次”,您会这样做
build_graph()with tf.Session() as sess: process_something()with tf.Session() as sess: process_something()
请注意,我只构建了一次图。图是事物外观的抽象表示,它不保存任何计算状态。当您尝试这样做时
build_graph()with tf.Session() as sess: process_something()build_graph()with tf.Session() as sess: process_something()
在第二次build_graph()时可能会因为尝试创建同名的变量(在您的情况下发生的情况)、图已完成等原因而出现错误。如果您确实需要以这种方式运行,您只需在中间重置图即可
build_graph()with tf.Session() as sess: process_something()tf.reset_default_graph()build_graph()with tf.Session() as sess: process_something()
这样就可以正常工作了。