ANN not training accurately as i am not getting a better loss reduction
Just starting up with regression and it seems i'm not getting something right please what am i doing wrong here as my loss is not reducing.
import torch
from torch import nn
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

df = pd.read_excel('Folds5x2_pp.xlsx')

x = df.iloc[:,:-1].values
y = df.iloc[:,-1].values

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)


class ANN(nn.Module):
  def __init__(self, input, output):
    super(ANN, self).__init__()
    self.fc1 = nn.Linear(input, 6)
    self.r1 = nn.ReLU()
    self.fc2 = nn.Linear(6, output)

  def forward(self, x):
    return self.fc2(self.r1(self.fc1(x)))


f, s = x.shape
ann = ANN(s, 1)

criterion = nn.MSELoss()
optimizer = torch.optim.Adam(ann.parameters(), lr=0.01)


x = torch.from_numpy(x_train.astype(np.float32))
y = torch.from_numpy(y_train.astype(np.float32))
for i in range(100):
  y_pred = ann(x)
  loss = criterion(y_pred, y)
  print(f&quot;i: {i}, loss: {loss.item()}&quot;)
  loss.backward()
  optimizer.step()
  optimizer.zero_grad()



You should put optimizer.zero_grad() first, because the gradient will be relative to the previous batch of data if you don't zero it out.
Like this:
for i in range(100):
  y_pred = ann(x)
  loss = criterion(y_pred, y)
  print(f&quot;i: {i}, loss: {loss.item()}&quot;)
  optimizer.zero_grad()
  loss.backward()
  optimizer.step()

