Created
September 3, 2019 09:08
-
-
Save hccho2/b80c628dd0bd7d883b02c2a70a63974a to your computer and use it in GitHub Desktop.
torch code
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
# coding: utf-8 | |
import torch | |
w_true = torch.Tensor([1, 2, 3]) | |
X = torch.cat([torch.ones(100, 1), torch.randn(100, 2)], 1) | |
Y = torch.mv(X, w_true) + torch.randn(100) * 0.5 | |
w = torch.randn(3, requires_grad=True) | |
lr = 0.1 | |
for epoc in range(100): | |
w.grad = None | |
y_pred = torch.mv(X, w) | |
loss = torch.mean((Y - y_pred)**2) | |
loss.backward() | |
w.data = w.data - lr * w.grad.data | |
if epoc%10==0: | |
print('step: {}, loss = {:.4f}'.format(epoc,loss.item())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment