天天看點

[Intro to Deep Learning with PyTorch -- L2 -- N20] Cross-Entropy

If I have bunch of events and a bunch of probabilities, how likely is it those events happen based on the probabilities?

If it is very likely then we have a small cross entropy; 

If it is unlikely, then we have a large cross entropy.

import numpy as np

# Write a function that takes as input two lists Y, P,
# and returns the float corresponding to their cross-entropy.
def cross_entropy(Y, P):
    Y = np.float_(Y)
    P = np.float_(P)
    return -np.sum(Y * np.log(P) + (1 - Y) * np.log(1 - P))