'''
Compare softmax_cross_entropy_with_logits and sparse_softmax_cross_entropy_with_logits.
Both functions should compute the same results. softmax_cross_entropy_with_logits computes
cross entropy using one-hot encoded labels. But sparse_softmax_cross_entropy_with_logits
computes the cross entropy directly on the sparse labels instead of converting them with
one-hot encoding.
'''
import tensorflow as tf
import numpy as np
# generate a batch of 10 labels. Each label is a number between 0 and 5.
batch_size = 10
dims = 5
labels = np.random.randint(0,dims,size=[batch_size])
print('labels=',labels)
# define a session
sess = tf.Session()
logits = tf.random_uniform([batch_size,dims], maxval=3, dtype=tf.float32)
print('logits=',logits.eval(session=sess))
one_hot_labels = tf.one_hot(labels, dims)
print('labels=',labels)
print('one-hot labels=',one_hot_labels.eval(session=sess))
cross_entropy_1 = tf.nn.softmax_cross_entropy_with_logits( logits=logits, labels=one_hot_labels)
cross_entropy_2 = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=tf.constant(labels))
ce_1, ce_2 = sess.run([cross_entropy_1, cross_entropy_2])
print('cross entropy 1 = ',ce_1)
print('cross entropy 2 = ',ce_2)
print(ce_1 == ce_2)