I am performing simple linear regression using PyTorch but my model is not able to properly fit over the training data. please look at the code to find the mistake.
import torch
from torch import nn
from torch.optim import SGD, Adam
from torch.autograd import Variable
class Linear_Reg(nn.Module):
def __init__(self):
super(Linear_Reg, self).__init__()
self.linear = nn.Linear(1,1)
def forward(self, x):
y_pred = self.linear(x)
return y_pred
net = Linear_Reg()
Xt = Variable(torch.Tensor(X[:,0]))
yt = Variable(torch.Tensor(y))
Xt = Xt.view(-1,1)
criterion = nn.MSELoss()
optimizer = Adam(net.parameters(), lr=0.001)
EPOCHS = 500
for epoch in range(EPOCHS):
pred_y = net(Xt)
loss = criterion(pred_y, yt)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('Eopch: {}, \t\t loss: {}'.format(epoch, loss.data.item()))
The loss decreases from ~68.88 to ~30.26
and the resulting fitting is this:
xxt = torch.arange(5,23)
with torch.no_grad():
a = net(xxt.reshape(-1,1).float())
plt.scatter(X[:,1], y, s=30, c='r', marker='x', linewidths=1)
plt.plot(xxt.data.numpy(),a.data.numpy(), label='Linear regression (Gradient descent)')
What is the problem here?