Hi @ptrblck
Thanks for the reply.
I modified the code  below
# Setting the number of epochs

losses = []

tlosses = []

nb_epoch = 200

# Iterating over each epoch

for epoch in range(1, nb_epoch + 1):

    #sae.train()

    # Initializing the train_loss which will be updated

    train_loss = 0

    test_loss = 0

    # Initializing a counter

    s = 0.

    # Iterating over each user

    #for id_user in range(nb_users):

    for batch_idx, (sample) in enumerate(train_loader):

        # The input corresponds to the ratings given by the current user for each movie

        input = Variable(sample)

        target = input.clone()

        # We don't consider movies NOT rated by the current user. So we specify a conditional statement

        if torch.sum(target.data > 0) > 0:

            # We use our SAE to get the output from the 

            #print('input:  '+ str(input.shape))

            output = sae(input)

            #print(output.shape)

            target.require_grad = False

            output[target == 0] = 0

            # Defining our loss function, comparing the output with the target

            loss = criterion(output, target)

            mean_corrector = nb_movies/float(torch.sum(target.data > 0) + 1e-10)

            # Computing the gradients necessary to adjust the weights

            loss.backward()

            # Updating the train_loss

            train_loss += np.sqrt(loss.data*mean_corrector)

            s += 1.

            # Updating the weights of the neural network

            optimizer.step()

            

    with torch.no_grad():

      for batch_idx, (sample) in enumerate(test_loader):

          # The input corresponds to the ratings given by the current user for each movie

          input = Variable(sample)

          target = input.clone()

          # We don't consider movies NOT rated by the current user. So we specify a conditional statement

          if torch.sum(target.data > 0) > 0:

              # We use our SAE to get the output from the 

              #print('input:  '+ str(input.shape))

              output = sae(input)

              #print(output.shape)

              target.require_grad = False

              output[target == 0] = 0

              # Defining our loss function, comparing the output with the target

              loss = criterion(output, target)

              mean_corrector = nb_movies/float(torch.sum(target.data > 0) + 1e-10)

              # Computing the gradients necessary to adjust the weights

              # Updating the train_loss

              test_loss += np.sqrt(loss.data*mean_corrector)

              s += 1.

      

      epoch_loss = train_loss / len(train_loader)

      epoch_loss1 = test_loss / len(test_loader)

      losses.append(epoch_loss)

      tlosses.append(epoch_loss1)      

      print('epoch: '+str(epoch)+' Training loss: '+str(train_loss/s))

      print('epoch: '+str(epoch)+' Test loss: '+str(test_loss/s))




When I plot the training loss and Validation loss graphs .They look basically the same.
epoch: 1 Training loss: tensor(0.1306)
epoch: 1 Test loss: tensor(0.1298)
epoch: 2 Training loss: tensor(0.1296)
epoch: 2 Test loss: tensor(0.1285)
epoch: 3 Training loss: tensor(0.1289)
epoch: 3 Test loss: tensor(0.1275)
epoch: 4 Training loss: tensor(0.1277)
epoch: 4 Test loss: tensor(0.1263)
epoch: 5 Training loss: tensor(0.1265)
epoch: 5 Test loss: tensor(0.1252)
.........
.......
.......
epoch: 195 Training loss: tensor(0.0419)
epoch: 195 Test loss: tensor(0.0428)
epoch: 196 Training loss: tensor(0.0420)
epoch: 196 Test loss: tensor(0.0426)
epoch: 197 Training loss: tensor(0.0417)
epoch: 197 Test loss: tensor(0.0423)
epoch: 198 Training loss: tensor(0.0418)
epoch: 198 Test loss: tensor(0.0423)
epoch: 199 Training loss: tensor(0.0417)
epoch: 199 Test loss: tensor(0.0421)
epoch: 200 Training loss: tensor(0.0418)
epoch: 200 Test loss: tensor(0.0425)



I am new to Pytorch, can you(or someone) please have a look if I am doing it right?
Is the model fine (Not overfitting or undefitting)?
Thanks.