批量培训但在Tensorflow中测试单个数据项? [英] Training in batches but testing individual data item in Tensorflow?

查看:297
本文介绍了批量培训但在Tensorflow中测试单个数据项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经训练了一个批量大小为10的卷积神经网络. 但是,在测试时,我想分别而不是分批地预测每个数据集的分类,这会产生错误:

I have trained a convolution neural network with batch size of 10. However when testing, I want to predict the classification for each dataset separately and not in batches, this gives error:

Assign requires shapes of both tensors to match. lhs shape= [1,3] rhs shape= [10,3]

我理解10是指batch_size,而3是我要归类的类数.

I understand 10 refers to batch_size and 3 is the number of classes that I am classifying into.

我们不能批量训练并单独测试吗?

Can we not train using batches and test individually?

更新:

培训阶段:

batch_size=10
classes=3
#vlimit is some constant : same for training and testing phase
X = tf.placeholder(tf.float32, [batch_size,vlimit ], name='X_placeholder')
Y = tf.placeholder(tf.int32, [batch_size, classes], name='Y_placeholder')
w = tf.Variable(tf.random_normal(shape=[vlimit, classes], stddev=0.01), name='weights')
b = tf.Variable(tf.ones([batch_size,classes]), name="bias")
logits = tf.matmul(X, w) + b
entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y, name='loss')
loss = tf.reduce_mean(entropy)
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)

测试阶段:

batch_size=1
classes=3
X = tf.placeholder(tf.float32, [batch_size,vlimit ], name='X_placeholder')
Y = tf.placeholder(tf.int32, [batch_size, classes], name='Y_placeholder')
w = tf.Variable(tf.random_normal(shape=[vlimit, classes], stddev=0.01), name='weights')
b = tf.Variable(tf.ones([batch_size,classes]), name="bias")
logits = tf.matmul(X, w) + b
entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y, name='loss')
loss = tf.reduce_mean(entropy)
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)

推荐答案

定义占位符时,请使用:

When you define your placeholder, use:

X = tf.placeholder(tf.float32, [None, vlimit ], name='X_placeholder')
Y = tf.placeholder(tf.int32, [None, classes], name='Y_placeholder')
...

而不是在您的培训和测试阶段(实际上,您不需要在测试阶段重新定义它们).还将您的偏见定义为:

instead for both your training and testing phase (actually, you shouldn't need to re-define these for the testing phase). Also define your bias as:

b = tf.Variable(tf.ones([classes]), name="bias")

否则,您正在为批次中的每个样本训练一个单独的偏差,这不是您想要的.

Otherwise you are training a separate bias for each sample in your batch, which is not what you want.

TensorFlow应该自动沿输入的第一个维度展开,并将其识别为批次大小,因此,为了进行培训,您可以以10批为单位喂食,而进行测试时可以以单个样本(或100批次或任何批次)喂食.

TensorFlow should automatically unroll along the first dimension of your input and recognize that as the batch size, so for training you can feed it batches of 10, and for testing you can feed it individual samples (or batches of 100 or whatever).

这篇关于批量培训但在Tensorflow中测试单个数据项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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