import tensorflow as tf
print(tf.__version__)
tf.enable_eager_execution()
logits = tf.Variable(tf.truncated_normal(shape=(2, 5)))
labels = tf.Variable([[1, 0, 0, 0, 0], [0, 0, 0, 0, 1]], dtype=tf.float32)
"""
下面是实现交叉熵损失的两种方法:等价的两种方法
"""
# 第一种交叉熵实现方法
losses = tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits)
loss = tf.reduce_mean(losses)
print("loss: ", loss)
# 第二种交叉熵实现方法
log_softmax = tf.nn.log_softmax(logits=logits, axis=-1)
loss2 = -tf.reduce_mean(tf.reduce_sum(labels * log_softmax, axis=-1))
print("loss2: ", loss2)