Skip to content

Instantly share code, notes, and snippets.

@Mehdi-Amine
Last active June 2, 2020 23:03
Show Gist options
  • Save Mehdi-Amine/29795d4872cc0a0aa6aa10c61f445d07 to your computer and use it in GitHub Desktop.
Save Mehdi-Amine/29795d4872cc0a0aa6aa10c61f445d07 to your computer and use it in GitHub Desktop.
Sigmoid prime
import torch
#----------- Implementing the math -----------#
def sigmoid_prime(z):
return sigmoid(z) * (1 - sigmoid(z))
z = torch.tensor([[2.], [-3.]], requires_grad=True)
sig_p = sigmoid_prime(z)
#----------- Using Pytorch autograd -----------#
torch_sig.backward(torch.tensor([[1.], [1.]]))
#----------- Comparing outputs -----------#
print(f"Pytorch Sigmoid': \n{z.grad} \nOur Sigmoid': \n{sig_p}")
'''
Out:
Pytorch Sigmoid':
tensor([[0.1050],[0.0452]])
Our Sigmoid':
tensor([[0.1050],[0.0452]])
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment