Skip to content

Instantly share code, notes, and snippets.

@simrit1
Created April 10, 2022 17:34
Show Gist options
  • Select an option

  • Save simrit1/49df21cd7be2df4f83850ebcc607de26 to your computer and use it in GitHub Desktop.

Select an option

Save simrit1/49df21cd7be2df4f83850ebcc607de26 to your computer and use it in GitHub Desktop.
import numpy as np
def crossEntropy(Y, P):
Y = np.float_(Y)
P = np.float_(P)
CE = -np.sum(Y*np.log(P) + (1-Y)*np.log(1-P))
return CE
#cross entropy tells us when two vectors are similar or different.
#To compute the cross entropy simply calculate the negative of the natural logarithm of the product of probabilities.
#That is, given probabilities p1, p2, p3 then the cross entropy is -ln(p1*p2*p3) = -ln(p1)-ln(p2)-ln(p3)
#example: write a function that takes two lists and return the XEntropy
import numpy as np
def softmax(L):
exp_f = np.exp(L)
sum_exp = sum(exp_f)
result = []
for i in exp_f:
result.append(i*1.0/sum_exp)
return result
#softmax function replaces the sigmoid function when dealing with 3 or more classes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment