Created
June 4, 2019 04:36
-
-
Save yunjey/3c5487e3c888203a71b4f51264539902 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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