-
Notifications
You must be signed in to change notification settings - Fork 86
/
1_linear_regression.py
55 lines (40 loc) · 1.38 KB
/
1_linear_regression.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
import torch
from torch.autograd import Variable
from torch import optim
def build_model():
model = torch.nn.Sequential()
model.add_module("linear", torch.nn.Linear(1, 1, bias=False))
return model
def train(model, loss, optimizer, x, y):
model.train()
x = Variable(x, requires_grad=False)
y = Variable(y, requires_grad=False)
# Reset gradient
optimizer.zero_grad()
# Forward
fx = model.forward(x.view(len(x), 1)).squeeze()
output = loss.forward(fx, y)
# Backward
output.backward()
# Update parameters
optimizer.step()
return output.item()
def main():
torch.manual_seed(42)
X = torch.linspace(-1, 1, 101)
Y = 2 * X + torch.randn(X.size()) * 0.33
model = build_model()
loss = torch.nn.MSELoss(reduction='elementwise_mean')
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
batch_size = 10
for i in range(100):
cost = 0.
num_batches = len(X) // batch_size
for k in range(num_batches):
start, end = k * batch_size, (k + 1) * batch_size
cost += train(model, loss, optimizer, X[start:end], Y[start:end])
print("Epoch = %d, cost = %s" % (i + 1, cost / num_batches))
w = next(model.parameters()).data # model has only one parameter
print("w = %.2f" % w.numpy()) # will be approximately 2
if __name__ == "__main__":
main()