-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
60 lines (47 loc) · 1.52 KB
/
example.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from momo import Momo, MomoAdam
# Example neural network
class Net(nn.Module):
def __init__(self, d, H):
super(Net, self).__init__()
self.fc1 = nn.Linear(d, H)
self.fc2 = nn.Linear(H, 1)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
# Define a synthetic dataset
d = 2 # input dimension
N = 1000 # number of samples
H = 100 # hidden layer size
X = torch.randn(N, d)
y = (X**2).sum(axis=1)
ds = torch.utils.data.TensorDataset(torch.FloatTensor(X), torch.FloatTensor(y))
dl = DataLoader(ds, batch_size=10)
# Define the loss function
criterion = nn.MSELoss()
def loss_fn(output, labels):
loss = criterion(output.view(-1), labels.view(-1))
loss.backward()
return loss
def train(model, opt, epochs=100):
for epoch in range(epochs):
for input, labels in dl:
opt.zero_grad()
output = model(input)
loss = loss_fn(output, labels)
opt.step(loss=loss)
# alternative:
# closure = lambda: loss_fn(output, labels) # define a closure that return loss
# opt.step(closure=closure)
# print progress
if epoch % 10 == 0:
print('Epoch {}, loss: {}'.format(epoch, 1/N*((model(X).view(-1) - y)**2).sum()))
return
if __name__ == '__main__':
model = Net(d,H)
opt = Momo(model.parameters(), lr=1)
# opt = MomoAdam(model.parameters(), lr=1e-2)
train(model, opt, epochs=100)