Created
May 6, 2018 15:21
-
-
Save karszawa/162678dab6e9579fae78be3e4da36241 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 | |
from torch.autograd import Variable | |
N, D_in, H, D_out = 64, 1000, 100, 10 | |
x = Variable(torch.randn(N, D_in), requires_grad=False) | |
y = Variable(torch.randn(N, D_out), requires_grad=False) | |
w1 = Variable(torch.randn(D_in, H), requires_grad=True) | |
w2 = Variable(torch.randn(H, D_out), requires_grad=True) | |
learning_rate = 1e-6 | |
for t in range(500): | |
y_pred = x.mm(w1).clamp(min=0).mm(w2) | |
loss = (y_pred - y).pow(2).sum() | |
if w1.grad: w1.grad.data.zero_() | |
if w2.grad: w2.grad.data.zero_() | |
loss.backward() | |
w1.data -= learning_rate * w1.grad.data | |
w2.data -= learning_rate * w2.grad.data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment