输入占位符中的 Tensorflow 批量大小 [英] Tensorflow batch size in input placholder

查看:27
本文介绍了输入占位符中的 Tensorflow 批量大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Tensorflow 的新手,我不明白为什么输入占位符的尺寸通常与用于训练的批次的大小有关.

I am new to Tensorflow and I can't get why the input placeholder is often dimensioned with the size of the batches used for training.

在这个例子中,我找到了这里,在官方 Mnist 教程中,它是不是

In this example I found here and in the Official Mnist tutorial it is not

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}))

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

So what is the best and right way to dimension and create the model input and train it?

推荐答案

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

Here you are specifying the model input. You want to leave the Batch size, to None, that means that you can run the model with a variable number of inputs (one or more). Batching is important to efficiently use your computing resources.

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

下一个重要的行是:

batch = mnist.train.next_batch(50)

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

Here you are sending 50 elements as input but you can also change that to just one

batch = mnist.train.next_batch(1)

无需修改图表.如果您指定 Batch 大小(第一个代码段中的某个数字而不是 None),那么您每次都必须更改,这并不理想,特别是在生产中.

Without modifying the graph. If you specify the Batch size (some number instead of None in the first snippet), then you would have to change each time and that is not ideal, specially in production.

这篇关于输入占位符中的 Tensorflow 批量大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆