在 Tensorflow 中使用 Adadelta 优化器时出现未初始化值错误
Uninitialized value error while using Adadelta optimizer in Tensorflow
我正在尝试使用 Adagrad 优化器构建 CNN,但出现以下错误。
tensorflow.python.framework.errors.FailedPreconditionError: 试图使用未初始化的值 Variable_7/Adadelta
[[节点:Adadelta/update_Variable_7/ApplyAdadelta = ApplyAdadelta[T=DT_FLOAT, _class=["loc:@Variable_7"], use_locking=false, _device= "/工作:localhost/replica:0/任务:0/cpu:0"](Variable_7, Variable_7/Adadelta, Variable_7/Adadelta_1, Adadelta/lr , Adadelta/rho, Adadelta/epsilon, gradients/add_3_grad/tuple/control_dependency_1)]]
由op u'Adadelta/update_Variable_7/ApplyAdadelta',
引起
优化器=tf.train.AdadeltaOptimizer(learning_rate).最小化(cross_entropy)
我尝试按照 post 中提到的 adagrad 语句后重新初始化会话变量,但这也没有帮助。
如何避免这个错误?谢谢
import tensorflow as tf
import numpy
from tensorflow.examples.tutorials.mnist import input_data
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
# Parameters
learning_rate = 0.01
training_epochs = 100
batch_size = 1000
display_step = 1
# Set model weights
W = tf.Variable(tf.zeros([784, 10]), name="weights")
b = tf.Variable(tf.zeros([10]), name="bias")
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
# Initializing the variables
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):
total_batch = int(mnist.train.num_examples/batch_size)
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
x_image = tf.reshape(batch_xs, [-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
y_conv=tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(batch_ys * tf.log(y_conv), reduction_indices=[1]))
#optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)
optimizer = tf.train.AdadeltaOptimizer(learning_rate).minimize(cross_entropy)
sess.run(init)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(batch_ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run([cross_entropy, y_conv,optimizer])
print cross_entropy.eval()
这里的问题是 tf.initialize_all_variables()
是一个误导性的名称。真的是"return an operation that initializes all variables that have already been created (in the default graph)"的意思。当您调用 tf.train.AdadeltaOptimizer(...).minimize()
时,TensorFlow 会创建 其他 变量,这些变量未包含在您之前创建的 init
操作中。
移动线路:
init = tf.initialize_all_variables()
...在 构建 tf.train.AdadeltaOptimizer
后,您的程序应该可以运行。
N.B. 你的程序会在每个训练步骤重建整个网络,除了变量。这可能非常低效,并且 Adadelta 算法不会按预期进行调整,因为它的状态在每一步都被重新创建。我强烈建议将代码从 batch_xs
的定义移动到两个嵌套 for
循环之外的 optimizer
的创建。您应该为 batch_xs
和 batch_ys
输入定义 tf.placeholder()
操作,并使用 feed_dict
参数给 sess.run()
以传递 [=23= 返回的值].
我正在尝试使用 Adagrad 优化器构建 CNN,但出现以下错误。
tensorflow.python.framework.errors.FailedPreconditionError: 试图使用未初始化的值 Variable_7/Adadelta
[[节点:Adadelta/update_Variable_7/ApplyAdadelta = ApplyAdadelta[T=DT_FLOAT, _class=["loc:@Variable_7"], use_locking=false, _device= "/工作:localhost/replica:0/任务:0/cpu:0"](Variable_7, Variable_7/Adadelta, Variable_7/Adadelta_1, Adadelta/lr , Adadelta/rho, Adadelta/epsilon, gradients/add_3_grad/tuple/control_dependency_1)]] 由op u'Adadelta/update_Variable_7/ApplyAdadelta',
引起优化器=tf.train.AdadeltaOptimizer(learning_rate).最小化(cross_entropy)
我尝试按照 post 中提到的 adagrad 语句后重新初始化会话变量,但这也没有帮助。
如何避免这个错误?谢谢
import tensorflow as tf
import numpy
from tensorflow.examples.tutorials.mnist import input_data
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
# Parameters
learning_rate = 0.01
training_epochs = 100
batch_size = 1000
display_step = 1
# Set model weights
W = tf.Variable(tf.zeros([784, 10]), name="weights")
b = tf.Variable(tf.zeros([10]), name="bias")
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
# Initializing the variables
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):
total_batch = int(mnist.train.num_examples/batch_size)
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
x_image = tf.reshape(batch_xs, [-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
y_conv=tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(batch_ys * tf.log(y_conv), reduction_indices=[1]))
#optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)
optimizer = tf.train.AdadeltaOptimizer(learning_rate).minimize(cross_entropy)
sess.run(init)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(batch_ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run([cross_entropy, y_conv,optimizer])
print cross_entropy.eval()
这里的问题是 tf.initialize_all_variables()
是一个误导性的名称。真的是"return an operation that initializes all variables that have already been created (in the default graph)"的意思。当您调用 tf.train.AdadeltaOptimizer(...).minimize()
时,TensorFlow 会创建 其他 变量,这些变量未包含在您之前创建的 init
操作中。
移动线路:
init = tf.initialize_all_variables()
...在 构建 tf.train.AdadeltaOptimizer
后,您的程序应该可以运行。
N.B. 你的程序会在每个训练步骤重建整个网络,除了变量。这可能非常低效,并且 Adadelta 算法不会按预期进行调整,因为它的状态在每一步都被重新创建。我强烈建议将代码从 batch_xs
的定义移动到两个嵌套 for
循环之外的 optimizer
的创建。您应该为 batch_xs
和 batch_ys
输入定义 tf.placeholder()
操作,并使用 feed_dict
参数给 sess.run()
以传递 [=23= 返回的值].