我在TensorFlow中成功实现了一个前馈算法,如下所示......
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
# tf Graph Input
x = tf.placeholder(tf.float32, [None, 784]) # mnist data image of shape 28*28=784
y = tf.placeholder(tf.float32, [None, 10]) # 0-9 digits recognition => 10 classes
# set model weights
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
# construct model
logits = tf.matmul(x, W) + b
pred = tf.nn.softmax(logits) # Softmax
# minimize error using cross entropy
cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1))
# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(cost)
# initializing the variables
init = tf.global_variables_initializer()
......培训周期如下......
# launch the graph
with tf.Session() as sess:
sess.run(init)
# training cycle
for epoch in range(FLAGS.training_epochs):
avg_cost = 0
total_batch = int(mnist.train.num_examples/FLAGS.batch_size)
# loop over all batches
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(FLAGS.batch_size)
_, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys})
......其余代码不是必需的。到目前为止,代码完美无缺。请务必注意,我的batch_size
为100.问题是我使用tf.placeholder
表示我的值,但实际上我需要将其更改为使用tf.get_variable
。我做的第一件事是更改以下内容......
# tf Graph Input
x = tf.get_variable("input_image", shape=[100,784], dtype=tf.float32)
y = tf.placeholder(shape=[100,10], name='input_label', dtype=tf.float32) # 0-9 digits recognition => 10 classes
# set model weights
W = tf.get_variable("weights", shape=[784, 10], dtype=tf.float32, initializer=tf.random_normal_initializer())
b = tf.get_variable("biases", shape=[1, 10], dtype=tf.float32, initializer=tf.zeros_initializer())
# construct model
logits = tf.matmul(x, W) + b
pred = tf.nn.softmax(logits) # Softmax
# minimize error using cross entropy
cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1))
# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(cost)
# initializing the variables
init = tf.global_variables_initializer()
......到目前为止一切顺利。但现在我正在尝试实施培训周期,这是我遇到问题的地方。我使用batch_size = 100
运行与上述完全相同的训练周期,我得到以下错误......
tensorflow.python.framework.errors_impl.InvalidArgumentError:节点GradientDescent / update_input_image / ApplyGradientDescent的输入0是从_recv_input_image_0传递的浮点数:0与预期的float_ref不兼容。
如何解决此问题?错误来自以下行...
_, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys})
答案 0 :(得分:1)
我不清楚为什么当您继续为其提供值时,需要将x
更改为tf.Variable
。有两种解决方法(不包括您可以将x
恢复为tf.placeholder()
的情况,如工作代码中所示):
由于optimizer
正在尝试将SGD更新应用于您正在提供的值(这会导致令人困惑的运行时类型错误),因此引发了错误。您可以在构建optimizer
时传递trainable=False
来阻止x
执行此操作:
x = tf.get_variable("input_image", shape=[100, 784], dtype=tf.float32,
trainable=False)
由于x
是一个变量,因此您可以在运行optimizer
之前,在单独的步骤中将图像分配给变量。
x = tf.get_variable("input_image", shape=[100, 784], dtype=tf.float32)
x_placeholder = tf.placeholder(tf.float32, shape=[100, 784])
assign_x_op = x.assign(x_placeholder).op
# ...
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(FLAGS.batch_size)
# Assign the contents of `batch_xs` to variable `x`.
sess.run(assign_x_op, feed_dict={x_placeholder: batch_xs})
# N.B. Now you do not need to feed `x`.
_, c = sess.run([optimizer, cost], feed_dict={y: batch_ys})
后一版本允许您对图像内容执行渐变下降(这可能是您首先要将其存储在变量中的原因)。