首页 文章

输入平板机中的Tensorflow批量大小

提问于
浏览
4

我是Tensorflow的新手,我无法理解为什么输入占位符的大小通常是用于训练的批量大小 .

在这个例子中,我找到了here,并且在官方的Mnist教程中它不是

from get_mnist_data_tf import read_data_sets
mnist = read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
sess.run(tf.initialize_all_variables())
y = tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
for i in range(1000):
  batch = mnist.train.next_batch(50)
  train_step.run(feed_dict={x: batch[0], y_: batch[1]})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

print(accuracy.eval(feed_dict={x: mnist.test.images,
                               y_: mnist.test.labels}))

那么,维度和创建模型输入并进行训练的最佳和正确方法是什么?

1 回答

  • 10

    在这里您指定模型输入 . 您希望将批量大小保留为 None ,这意味着您可以使用可变数量的输入(一个或多个)运行模型 . 批处理对于有效使用计算资源非常重要 .

    x = tf.placeholder("float", shape=[None, 784])
    y_ = tf.placeholder("float", shape=[None, 10])
    

    下一个重要的是:

    batch = mnist.train.next_batch(50)
    

    在这里,您将发送50个元素作为输入,但您也可以将其更改为仅一个

    batch = mnist.train.next_batch(1)
    

    不修改图形 . 如果指定批量大小(第一个片段中的某个数字而不是“无”),那么每次都必须更改,这不是理想的,特别是在 生产环境 中 .

相关问题