python - 第二次运行tensorflow时出错

标签 python tensorflow deep-learning artificial-intelligence

我正在尝试运行以下 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 pd
import numpy as np
from sklearn.preprocessing import StandardScaler
import tensorflow as tf

learning_rate = 0.001
training_epochs = 100

n_input = 9
n_output = 1

n_layer1_node = 100
n_layer2_node = 100

X_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 1
with 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 2
with 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 layer
with 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")

正如错误所暗示的那样,我尝试将 reuse=True 添加为 with tf.variable_scope(): 中的参数,但这同样不起作用。

我在 conda 环境中运行它。我在 Windows 10 中使用 Python 3.5 和 CUDA 8(但这并不重要,因为它未配置为在 GPU 中运行)。

最佳答案

这是 TF 工作原理的问题。人们需要了解 TF 有一个“隐藏”状态——正在构建的图形。大多数 tf 函数都会在此图中创建操作(例如每个 tf.Variable 调用、每个算术运算等)。另一方面,实际的“执行”发生在 tf.Session() 中。因此,您的代码通常如下所示:

build_graph()

with tf.Session() as sess:
  process_something()

因为所有实际变量、结果等都只留在 session 中,如果你想“运行两次”你会这样做

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

将正常工作。

关于python - 第二次运行tensorflow时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45592118/

相关文章:

python - 无法卸载 Tensorflow

amazon-web-services - 如何将 fsx forluster 输入到 Amazon Sagemaker?

python - 如何在 TensorFlow 2.0 中组合两个渐变带

python - TensorFlow 中损失函数 (MLP) 的奇怪 NaN 值

python - 将文件上传到 Python 中的 Google Cloud Storage 签名 URL

python - 在 Python 中使用 Regex 从 OR 运算符捕获替代字符串?

python - Python 与 ML 中的词法作用域

python - 来自 tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory 的 tf.data.Dataset?

machine-learning - Caffe:如果内存中只能容纳一小部分怎么办?

python - GAE : How long to wait for eventual consistency?