gpt4 book ai didi

python - Tensorflow,变量 W3 已经存在,不允许

转载 作者:太空狗 更新时间:2023-10-29 17:01:25 24 4
gpt4 key购买 nike

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

# Lab 11 MNIST and Convolutional Neural Network
import tensorflow as tf
import random
# import matplotlib.pyplot as plt

from tensorflow.examples.tutorials.mnist import input_data

#tf.set_random_seed(777) # reproducibility

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# Check out https://www.tensorflow.org/get_started/mnist/beginners for
# more information about the mnist dataset

# hyper parameters
learning_rate = 0.001
training_epochs = 15
batch_size = 100

# input place holders
X = tf.placeholder(tf.float32, [None, 784])
X_img = tf.reshape(X, [-1, 28, 28, 1]) # img 28x28x1 (black/white)
Y = tf.placeholder(tf.float32, [None, 10])

# L1 ImgIn shape=(?, 28, 28, 1)
W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01))
# Conv -> (?, 28, 28, 32)
# Pool -> (?, 14, 14, 32)
L1 = tf.nn.conv2d(X_img, W1, strides=[1, 1, 1, 1], padding='SAME')
L1 = tf.nn.relu(L1)
L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')

# L2 ImgIn shape=(?, 14, 14, 32)
W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01))
# Conv ->(?, 14, 14, 64)
# Pool ->(?, 7, 7, 64)
L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME')
L2 = tf.nn.relu(L2)
L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
L2_flat = tf.reshape(L2, [-1, 7 * 7 * 64])

# Final FC 7x7x64 inputs -> 10 outputs
W3 = tf.get_variable("W3", shape=[7 * 7 * 64, 10],
initializer=tf.contrib.layers.xavier_initializer())

当我第二次尝试运行代码时,出现错误: ValueError:变量 W3 已存在,不允许。您的意思是在 VarScope 中设置 reuse=True 吗?最初定义于:

我写的代码是 Spyder 3.1.4,我使用的是 Python 3.6、Windows7 和 Tensorflow 1.2.1

最佳答案

对我来说效果很好。如果您在 spyder 中运行它,它可能会在同一图表上多次运行脚本,在这种情况下,您将在每次运行时将 W3 变量添加到图表中。要修复,请在脚本开头重置图表。

tf.reset_default_graph()

关于python - Tensorflow,变量 W3 已经存在,不允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45951037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com