still got an error
---------------------------------------------------------------------------

RuntimeError                              Traceback (most recent call last)

<ipython-input-46-a044eacf08de> in <module>()
     21 
     22       optimizer.zero_grad()
---> 23       loss.backward(retain_graph=True)
     24       optimizer.step()
     25 

1 frames

/usr/local/lib/python3.6/dist-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
    125     Variable._execution_engine.run_backward(
    126         tensors, grad_tensors, retain_graph, create_graph,
--> 127         allow_unreachable=True)  # allow_unreachable flag
    128 
    129 

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [100, 400]], which is output 0 of TBackward, is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

i had changed to code to include the corrections too
import time
start = time.time()
epochs = 1
val_correct = []
val_loss = []
train_correct = []
train_loss = []
for i in range(epochs):
  trn_corr = 0
  trn_loss = 0
  for b, (x_train,y_train) in enumerate(train_loader):
    with torch.autograd.set_detect_anomaly(True):
      b+=1
    #y_pred = model(x_train.cuda())
      y_pred = model(x_train)
    #y_train = y_train.cuda()
      loss = criterion(y_pred,y_train)
      predicted = torch.max(y_pred.data,1)[1] #gives the indices of the highest number in each row
      predicted = predicted.detach()
      trn_corr += (predicted == y_train).sum()

      optimizer.zero_grad()
      loss.backward(retain_graph=True)
      optimizer.step()

      if(b%100==0):
        print(f"Epoch:  {i}   batch:   {b}   loss   {loss.item()}   accuracy   {trn_corr.item()/(b*50)}")
    
  train_loss.append(loss)
  train_correct.append(trn_corr)


print(time.time() - start)

Thanks for the response i think  its some error with the forward network i am not able to figure out what it is