I am trying to train a simple model. However, it seems that the model does not learn anything, and I am unsure why I have this problem.
Here is my code:
class Regressor(nn.Module):
def __init__(self, inp_dim=1, out_dim=1, hidden_units=20):
super(Regressor, self).__init__()
self.criterion = torch.nn.MSELoss()
self.model = torch.nn.Sequential(
torch.nn.Linear(inp_dim, hidden_units),
torch.nn.ReLU(),
torch.nn.Linear(hidden_units, hidden_units),
torch.nn.ReLU(),
torch.nn.Linear(hidden_units, out_dim),
)
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=0.1)
def update(self, x, y_target):
y_pred = self.model(torch.Tensor(x))
loss = self.criterion(y_pred, torch.Tensor(y_target))
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
def predict(self, x):
with torch.no_grad():
return self.model(torch.Tensor(x))
And the data and training loop is as follows:
x = torch.linspace(1, 20, 64)
y = 2 * x + x ** 2 + np.random.rand()
model = Regressor()
epochs = 1000
for epoch in range(epochs):
model.update(x.unsqueeze(-1), y)
I would be grateful for any help.