TensorFlow 变量配置

TensorFlow variable configuration

我在 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: Input 0 of node GradientDescent/update_input_image/ApplyGradientDescent was passed float from _recv_input_image_0:0 incompatible with expected float_ref.

我该如何解决这个问题?错误来自以下行...

_, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys})

我不清楚为什么在继续为它提供值时需要将 x 更改为 tf.Variable。有两种解决方法(不包括您可以将 x 恢复为工作代码中的 tf.placeholder() 的情况):

  1. 出现错误是因为 optimizer 试图将 SGD 更新应用到您输入的值(这会导致令人困惑的运行时类型错误)。您可以通过在构造 x:

    时传递 trainable=False 来阻止 optimizer 执行此操作
    x = tf.get_variable("input_image", shape=[100, 784], dtype=tf.float32,
                        trainable=False)
    
  2. 由于 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})
    

    后一个版本将允许您对图像的内容执行梯度下降(这可能是您首先要将其存储在变量中的原因)。