Skip to content

Instantly share code, notes, and snippets.

@yunjey
Created June 4, 2019 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yunjey/3c5487e3c888203a71b4f51264539902 to your computer and use it in GitHub Desktop.
Save yunjey/3c5487e3c888203a71b4f51264539902 to your computer and use it in GitHub Desktop.
import torch
x1 = torch.tensor(1.).requires_grad_(True)
x2 = torch.tensor(2.).requires_grad_(True)
w = torch.tensor(3.).requires_grad_(False)
y1 = w * x1
loss1 = torch.mean((y1-1)**2)
loss1.backward()
print(w.grad) # None
w.requires_grad_(True)
y2 = w * x2
loss2 = torch.mean((y2-1)**2)
loss2.backward()
print(w.grad) # 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment