machine-learningtensorflowmini-batch

Tensorflow- How can I use MNIST Dataset with full batch?


I'm studying about machine learning. While I'm studying, I found Tensorflow CNN code using MNIST Dataset.And here's a code that i want to know.

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.global_variables_initializer())

for i in range(1000):
  batch = mnist.train.next_batch(100)
   if i%100 == 0:
     train_accuracy = accuracy.eval(feed_dict={
        x:batch[0], y_: batch[1], keep_prob: 1.0})
    print("step %d, training accuracy %g"%(i, train_accuracy))
  train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

print("test accuracy %g"%accuracy.eval(feed_dict={
     x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

In this code, my question is about batch = mnist.train.next_batch(100). When I searched about this, it means that this is mini-batch and randomly choose 100 data from MNIST dataset. Now here's my question.

  1. When I want to test this code with full batch, what should I do? Just change mnist.train.next_batch(100) to mnist.train.next_batch(55000)?

Solution

  • Yes, getting a batch of 55000 will train one epoch on all digits of MNIST.

    Note that this is a bad idea: this will likely not fit into your memory. You would have to save the weight activation of 55000 digits, and the gradients... it is very likely your Python will crash!

    By training 1000 times on a batch of 100 random images you get a great result, and your computer is happy!