Created
February 24, 2023 12:13
-
-
Save enhuiz/335fd95c521ae11a3a8eb5c33a77e231 to your computer and use it in GitHub Desktop.
Use Adam in a wrong way.
This file contains hidden or 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 matplotlib.pyplot as plt | |
| import torch | |
| from celluloid import Camera | |
| from torch import nn | |
| linear = nn.Linear(2, 1, bias=False) | |
| camera = Camera(plt.figure()) | |
| optimizer = torch.optim.Adam(linear.parameters(), 1) | |
| x = torch.randn(1, 2) | |
| optimizer.zero_grad() | |
| y = linear(x) | |
| y.sum().backward() | |
| optimizer.step() | |
| plt.scatter(*linear.weight.tolist()[0]) | |
| camera.snap() | |
| for _ in range(10): | |
| optimizer.zero_grad() | |
| optimizer.step() | |
| plt.scatter(*linear.weight.tolist()[0], color='blue') | |
| plt.xlim(-5, 5) | |
| print(linear.weight) | |
| camera.snap() | |
| animation = camera.animate() | |
| animation.save("test.mp4") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment