-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinear_regression.py
55 lines (34 loc) · 1.06 KB
/
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
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_regression
X_num, y_num = make_regression(n_samples=200, n_features=1, noise=20, random_state=42)
#plt.scatter(X[:,0], y)
#plt.show()
X = torch.from_numpy(X_num.astype(np.float32))
y = torch.from_numpy(y_num.astype(np.float32))
y = y.view(y.shape[0], 1)
n_samples, n_features = X.shape
# 2) loss and optimiser
input_size = n_features
output_size = 1
model = nn.Linear(input_size, output_size)
learning_rate = 0.01
loss_func = nn.MSELoss()
optimiser = torch.optim.SGD(model.parameters(), lr=learning_rate)
# 4) train on the data in loop
num_epochs = 500
for epoch in range(num_epochs):
#forward pass and loss
y_predicted = model(X)
loss = loss_func(y_predicted, y)
loss.backward()
optimiser.step()
optimiser.zero_grad()
if (epoch + 1) % 10 == 0:
print("epoch", epoch, " loss: ", f'{loss.item():.4}')
predicted = model(X).detach()
plt.scatter(X, y)
plt.plot(X, predicted)
plt.show()