我是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}))
那么维度和创建模型输入并训练它的最佳和正确方法是什么?
答案 0 :(得分: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)
不修改图表。如果您指定批量大小(第一个代码段中的某个数字而不是“无”),那么您每次都必须更改,这是不理想的,特别是在生产中。