Skip to content

Instantly share code, notes, and snippets.

@Mehdi-Amine
Created June 5, 2020 13:31
Show Gist options
  • Save Mehdi-Amine/5f297cb7ab09babc402eb3a7cf9bdf97 to your computer and use it in GitHub Desktop.
Save Mehdi-Amine/5f297cb7ab09babc402eb3a7cf9bdf97 to your computer and use it in GitHub Desktop.
Implementing feedforward
y = torch.tensor([2])
x = torch.tensor([[0.9, 0.5, 0.3]])
w = torch.tensor([[0.2, 0.1, 0.4], [0.5, 0.6, 0.1], [0.1, 0.7, 0.2]], requires_grad=True)
b = torch.tensor([[0.1, 0.2, 0.1]], requires_grad=True)
#----------- Using our functions -----------#
z = x @ w.T + b # = [[0.4500, 0.9800, 0.6000]]
sm = softmax(z) # = [[0.2590, 0.4401, 0.3009]]
ce = cross_entropy(sm, y) # = 1.2009
#----------- Equivalent Pytorch -----------#
t_ce = F.cross_entropy(z, y) # = 1.2009
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment