Skip to content

Instantly share code, notes, and snippets.

@Mehdi-Amine
Last active June 4, 2020 00:52
Show Gist options
  • Save Mehdi-Amine/c1c7488eaa80b1940be20fd9dc0ef97e to your computer and use it in GitHub Desktop.
Save Mehdi-Amine/c1c7488eaa80b1940be20fd9dc0ef97e to your computer and use it in GitHub Desktop.
differentiating cross entropy
import torch
zs = torch.tensor([[0.1, 0.4, 0.2], [0.3, 0.9, 0.6]]) # The values of 3 output neurons for 2 instances
activations = softmax(zs) # = [[0.2894, 0.3907, 0.3199],[0.2397, 0.4368, 0.3236]]
y = torch.tensor([2,0]) # equivalent to [[0,0,1],[1,0,0]]
#----------- Implementing the math -----------#
def crossentropy_prime(activations, labels):
n = labels.shape[0]
activs = torch.zeros_like(activations)
activs[range(n), labels] = -1 / activations[range(n), labels] # integer array indexing
return activs
c_p = crossentropy_prime(activations, y)
#----------- Printing Output -----------#
print(f"Cross-entropy differentiation: \n{c_p}\n")
'''
Out:
Cross-entropy differentiation:
tensor([[ 0.0000, 0.0000, -3.1262],
[-4.1720, 0.0000, 0.0000]])
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment