Skip to content

Instantly share code, notes, and snippets.

@mahimairaja
Created April 24, 2024 04:10
Show Gist options
  • Save mahimairaja/02a12c60136c72dcdd790ac72dc6fe2a to your computer and use it in GitHub Desktop.
Save mahimairaja/02a12c60136c72dcdd790ac72dc6fe2a to your computer and use it in GitHub Desktop.
import torch
x = torch.ones(3) # Input Tensor
y = torch.zeros(2) # Expected Output Tensor
w = torch.randn(3, 2, requires_grad=True) # Weight matrix
b = torch.randn(2 , requires_grad=True) # Bias
z = torch.matmul(x, w) + b # Calculated output
loss = torch.nn.functional.binary_cross_entropy_with_logits(z, y)
loss.backward() # Computes the gradient of loss w.r.t w and b
print(w.grad)
print(b.grad)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment