-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn_model.py
More file actions
32 lines (25 loc) · 729 Bytes
/
Copy pathnn_model.py
File metadata and controls
32 lines (25 loc) · 729 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import torch
from torch.autograd import Variable
N, D_in, H, D_out = 64, 1000, 100, 10
x = Variable(torch.randn(N,D_in))
y = Variable(torch.randn(N,D_out),requires_grad=False)
model = torch.nn.Sequential(
torch.nn.Linear(D_in,H),
torch.nn.ReLU(),
torch.nn.Linear(H,D_out),
)
loss_fn = torch.nn.MSELoss(size_average=False)
learning_rate = 1e-6
for t in range(50000):
y_pred = model(x)
loss = loss_fn(y_pred,y)
print(t,loss.data[0])
model.zero_grad()
loss.backward()
for param in model.parameters():
param.data -= learning_rate * param.grad.data
# print(t,loss.data[0])
# model.zero_grad()
# loss.backward()
# for param in model.parameters():
# param.data -= learning_rate * param.grad.data