简体   繁体   English

批量培训但是在Tensorflow中测试单个数据项?

[英]Training in batches but testing individual data item in Tensorflow?

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: 我已经训练了一个批量大小为10的卷积神经网络。但是在测试时,我想分别预测每个数据集的分类而不是分批预测,这给出了错误:

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

I understand 10 refers to batch_size and 3 is the number of classes that I am classifying into. 我理解10指的是batch_size ,3指的是我分类的类数。

Can we not train using batches and test individually? 我们不能使用批次进行培训并单独测试吗?

Update: 更新:

Training Phase: 培训阶段:

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)

Testing Phase: 测试阶段:

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)

Absolutely. 绝对。 Placeholders are 'buckets' that get fed data from your inputs. 占位符是“桶”,可以从输入中获取数据。 The only thing they do is direct data into your model. 他们唯一要做的就是将数据直接导入模型。 They can act like 'infinite buckets' using the None trick - you can chuck as much (or as little) data into them as you want (depending on available resources obviously). 它们可以像使用None技巧一样充当“无限桶” - 您可以根据需要将尽可能多(或少量)的数据放入其中(显然取决于可用资源)。

In training, try replacing batch_size with None for the Training placeholders: 在培训中,尝试使用None替换batch_size for Training占位符:

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

Then define everything else you have as before. 然后像以前一样定义你拥有的其他一切。

Then do some training ops, for example: 然后做一些训练操作,例如:

 _, Tr_loss, Tr_acc = sess.run([optimizer, loss, accuracy], feed_dict{x: btc_x, y: btc_y})

For testing, re-use these same placeholders ( X , Y ) and don't bother redefining the other variables. 要进行测试,请重复使用这些相同的占位符( XY ),并且不要重新定义其他变量。

All Tensorflow variables are static for a single Tensorflow graph definition. 对于单个Tensorflow图定义,所有Tensorflow变量都是静态的。 If you're restoring the model, then the placeholders still exist from when it was trained. 如果要恢复模型,则占位符在训练时仍然存在。 As will the other variables eg w , b , logits , entropy & optimizer . 与其他变量一样,例如wblogitsentropyoptimizer

Then do some testing op, for example: 然后做一些测试操作,例如:

 Ts_loss, Ts_acc = sess.run( [loss, accuracy], feed_dict{ x: test_x , y: test_y } )

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 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应自动沿输入的第一维展开,并将其识别为批量大小,因此对于培训,您可以批量生产10个,并且对于测试,您可以为其提供单个样品(或100个或其他批次)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM