Tensorflow minibatch 训练 [英] Tensorflow minibatch training
问题描述
如何使用小批量数据在 TensorFlow 中训练网络?在 Deep-MNIST 教程中,他们使用:
How can I train a network in TensorFlow using minibatches of data? In the Deep-MNIST tutorial, they use:
for i in range(1000):
batch = mnist.train.next_batch(50)
train_step.run(feed_dict={x: batch[0], y_: batch[1]})
我的问题是 - x
和 y_
变量的维度是否适合单个示例,以及 batch[0]
,batch[1]
是这些输入和输出的列表吗?在这种情况下,TensorFlow 会自动为这些列表中的每个训练示例添加梯度吗?或者我应该创建我的模型以便 x
和 y_
获得整个 minibatch?
My question is - are x
and y_
variables with dimensions suitable to a single example, and batch[0]
,batch[1]
are lists of such inputs and outputs? in this case, will TensorFlow automatically add the gradients for each training example in these lists? or should I create my model so that x
and y_
get an entire minibatch?
我的问题是,当我尝试为每个占位符提供一个列表时,它会尝试输入占位符的整个列表,因此我得到大小不匹配:Cannot feed value of shape (n, m) 对于 Tensor u'ts:0',其形状为 '(m,)'
,其中 n
是小批量大小,m
是单个输入尺寸.
My problem is that when I try to feed it a list for each placeholder, it tries to input the entire list for the placeholder, and I therefore get a size mismatch: Cannot feed value of shape (n, m) for Tensor u'ts:0', which has shape '(m,)'
, where n
is the minibatch size and m
is the individual input size.
谢谢.
推荐答案
在 MNIST 教程中 x
和 y_
是具有定义形状的占位符:
In the MNIST tutorial x
and y_
are placeholders with a defined shape:
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
shape=[None, 784]
表示这个占位符有二维.
The shape=[None, 784]
means that this placeholder have 2 dimension.
所以,回答你的第一个问题:
So, to answer your first question:
是具有适合单个示例的维度的 x 和 y_ 变量
are x and y_ variables with dimensions suitable to a single example
第一个维度可以包含未定义数量的元素(so, 1, 2, ... 50 ...),第二个维度可以包含 784 = 28*28 个元素(这是单个 MNIST 的特征图片).
The first dimension can contain an undefined number of elements (so, 1, 2, ... 50 ...) and the second dimension can contain exaclly 784 = 28*28 elements (that are the features of a single MNIST image).
如果您使用形状为 [1, 784] 或 [50, 784] 的 Python 列表来提供图形,则 tensorflow 完全相同,它可以毫无问题地处理它.
If you feed the graph with a python list with shape [1, 784] or [50, 784] is totally the same for tensorflow, it can handle it without any problem.
batch[0],batch[1] 是这些输入和输出的列表吗?在教程中,他们定义了批处理调用batch = datasets.train.next_batch(50)
.因此:
batch[0],batch[1] are lists of such inputs and outputs? in the tutorial they define batch calling
batch = datasets.train.next_batch(50)
. Thus:
- batch[0] 是一个形状为 [50, 784] 的列表
- batch[1] 是一个形状为 [50, 10] 的列表
TensorFlow 会自动为这些列表中的每个训练示例添加梯度吗?或者我应该创建我的模型,以便 x 和 y_ 获得整个小批量?
will TensorFlow automatically add the gradients for each training example in these lists? or should I create my model so that x and y_ get an entire minibatch?
Tensorflow 会为您处理这个问题.
Tensorflow handles this for you.
您报告的错误 无法为 Tensor u'ts:0' 提供形状 (n, m) 的值,其形状为 '(m,)'
是形状不匹配错误.
The error you're reporting Cannot feed value of shape (n, m) for Tensor u'ts:0', which has shape '(m,)'
is a shape mismatch error.
您并没有将输入重塑为具有与占位符相同的形状.
You're not reshaping the inputs to have the same shape of the placeholder.
这篇关于Tensorflow minibatch 训练的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!