I was looking at the Tensorflow MNIST example for beginners and found that in this part:
for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(1
Nan occurs when 0*log(0) occurs:
replace:
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
with:
cross_entropy = -tf.reduce_sum(y_*tf.log(y + 1e-10))