import numpy as np def cross_entropy(p, q): return -sum([p[i] * np.log(q[i]) for i in range(len(p))]) p = [0, 0, 0, 1] q = [0.45, 0.2, 0.02, 0.33] cross_entropy(p, q)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter