import sklearn
from sklearn.datasets import load_boston
import torch
import torch.nn as nn
boston = load_boston()
num_epochs = 10
linear_model = nn.Linear(13,1,bias = True)
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(linear_model.parameters(), lr=0.01)
for epoch in range(num_epochs):
inputs = torch.from_numpy(boston.data)
targets = torch.from_numpy(boston.target)
targets = targets.float()
inputs = inputs.float()
outputs = linear_model(inputs)
loss = criterion(outputs,targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('epoch:{}/{} ...... loss:{:.4f}'.format(epoch,num_epochs,loss.item()))
The problem with your code seems to be data normalization, or, the lack of it.
I edited your code to add data normalization ((x - mean) / std), set epochs to 50 and switched the optimiser to Adam so it converges faster.
import sklearn
from sklearn.datasets import load_boston
import torch
import numpy as np
import torch.nn as nn
def normalize(X):
mean = np.mean(X)
std = np.std(X)
return ((X - mean) / std), mean, std
boston = load_boston()
m_in = np.zeros(13)
s_in = np.zeros(13)
b_in = boston.data
b_out = boston.target
for i in range(13):
b_in[:, i], m_in[i], s_in[i] = normalize(b_in[:, i])
b_out, m_out, s_out = normalize(b_out)
num_epochs = 50
linear_model = nn.Linear(13, 1, bias=True)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(linear_model.parameters(), lr=0.01)
linear_model.train()
for epoch in range(num_epochs):
inputs = torch.from_numpy(b_in)
targets = torch.from_numpy(b_out)
targets = targets.float().unsqueeze(1)
inputs = inputs.float()
outputs = linear_model(inputs)
loss = criterion(outputs, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('epoch: {}/{} ...... loss: {:.4f}'.format(epoch, num_epochs, loss.item()))
The new output looks like this:
epoch: 0/50 ...... loss: 1.2610
epoch: 1/50 ...... loss: 1.1395
epoch: 2/50 ...... loss: 1.0332
epoch: 3/50 ...... loss: 0.9421
epoch: 4/50 ...... loss: 0.8660
epoch: 5/50 ...... loss: 0.8040
epoch: 6/50 ...... loss: 0.7548
epoch: 7/50 ...... loss: 0.7164
epoch: 8/50 ...... loss: 0.6863
epoch: 9/50 ...... loss: 0.6623
epoch: 10/50 ...... loss: 0.6421
epoch: 11/50 ...... loss: 0.6240
epoch: 12/50 ...... loss: 0.6068
epoch: 13/50 ...... loss: 0.5895
epoch: 14/50 ...... loss: 0.5717
epoch: 15/50 ...... loss: 0.5532
epoch: 16/50 ...... loss: 0.5339
epoch: 17/50 ...... loss: 0.5143
epoch: 18/50 ...... loss: 0.4946
epoch: 19/50 ...... loss: 0.4752
epoch: 20/50 ...... loss: 0.4565
epoch: 21/50 ...... loss: 0.4389
epoch: 22/50 ...... loss: 0.4226
epoch: 23/50 ...... loss: 0.4079
epoch: 24/50 ...... loss: 0.3949
epoch: 25/50 ...... loss: 0.3835
epoch: 26/50 ...... loss: 0.3736
epoch: 27/50 ...... loss: 0.3652
epoch: 28/50 ...... loss: 0.3580
epoch: 29/50 ...... loss: 0.3517
epoch: 30/50 ...... loss: 0.3461
epoch: 31/50 ...... loss: 0.3410
epoch: 32/50 ...... loss: 0.3361
epoch: 33/50 ...... loss: 0.3315
epoch: 34/50 ...... loss: 0.3270
epoch: 35/50 ...... loss: 0.3226
epoch: 36/50 ...... loss: 0.3183
epoch: 37/50 ...... loss: 0.3142
epoch: 38/50 ...... loss: 0.3103
epoch: 39/50 ...... loss: 0.3068
epoch: 40/50 ...... loss: 0.3036
epoch: 41/50 ...... loss: 0.3008
epoch: 42/50 ...... loss: 0.2984
epoch: 43/50 ...... loss: 0.2963
epoch: 44/50 ...... loss: 0.2945
epoch: 45/50 ...... loss: 0.2930
epoch: 46/50 ...... loss: 0.2917
epoch: 47/50 ...... loss: 0.2904
epoch: 48/50 ...... loss: 0.2892
epoch: 49/50 ...... loss: 0.2881
The features in the dataset seem to have different ranges and this poses a problem for the learning algorithm. By normalizing the data, we make it much easier for the model to learn.
[1] This is a great article that explains why normalization is needed: https://towardsdatascience.com/understand-data-normalization-in-machine-learning-8ff3062101f0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.