Oh. I didn’t notice this before but there are a few more misstakes in your Run testing batches code part.
So. here:



 Ali_Mashoud:

#Run testing batches
with torch.no_grad():
        for b, (X_test,y_test) in enumerate(test_loader):
            b+=1
            y_val = model(y_test)
            loss = criterion(y_val,y_test)
            predicted = torch.max(y_val, 1)[1]
            batch_corr = (predicted==y_val).sum()
            tst_corr += batch_corr
        
            optimzer.zero_grad()
            loss.backward()
            optimzer.step()
            
        loss=loss.detach().numpy()
        test_losses.append(loss)
        test_correct.append(tst_corr)



You are doing testing and not training like above. You are already correctly using with torch.no_grad(): which turns off grad calculation for all tensors. Using this during testing is correct.
But you are also doing back propagation and optimizer steps.



 Ali_Mashoud:

            optimzer.zero_grad()
            loss.backward()
            optimzer.step()



This is normally not done during testing since your network is not supposed to learn from test-data.
The error comes from loss.backward() requiring grad calculation to be turned on which you turned off with with torch.no_grad():.
Remove these three lines
            optimzer.zero_grad()
            loss.backward()
            optimzer.step()

while keeping the with torch.no_grad(): and it should work.
(only for what is under #Run testing batches! under #Run the training batches you of course keep those lines)