Cross Entropy - pai-plznw4me/tensorflow_basic GitHub Wiki
Tensorflow
Low api
def crossentropy(y, y_hat):
y = tf.clip_by_value(y, 1e-15, 0.99)
y_hat = tf.clip_by_value(y_hat, 1e-15, 0.99)
cee = tf.math.reduce_sum(-(y * tf.math.log(y_hat)), axis=1)
mean_cee = tf.reduce_mean(cee)
return mean_loss
High api
def cee(y, y_hat):
return tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_hat))
Keras
cce = tf.keras.losses.CategoricalCrossentropy()
cce(y_true, y_pred).numpy()