Hi, it is like this:
loss_function = torch.nn.MSELoss(reduction='mean')
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.9)
# optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)

for epoch in range(total_epoch_number):
    loss_array_for_current_epoch = []
    overall_training_loss_for_current_epoch = 0
    for batch_num in range(number_of_training_data // N):

        current_batch_training_input = training_set_input[batch_num*100:batch_num*100+100, :].to(device)
        y_pred = model(current_batch_training_input)

        # Compute loss for current batch
        current_batch_training_output = training_set_output[batch_num*100:batch_num*100+100, :].to(device)
        loss = loss_function(y_pred, current_batch_training_output)
        
        
        
        
        # Zero gradients, perform a backward pass, and update the weights.    
        loss.backward()
        optimizer.step()
        optimizer.zero_grad()
        
        loss_array_for_current_epoch.append(loss.item())
        
    # Use the average loss of all batch as the overall training loss for this epoch.    
    overall_training_loss_for_current_epoch = np.average(loss_array_for_current_epoch)   
    
    # Validation process at current epoch
    with torch.no_grad():
        y_pred_val = model(validation_set_input)
        val_loss = loss_function(y_pred_val, validation_set_output)