Output evaluation loss after every n-batches instead of epochs with pytorch
Instead of printing the evaluation loss every epoch I would like to output it after every n-batches.
I have around 150'000 batches per epoch. I would like to output the evaluation loss every 50'000 batches.
Is this even possible? I am using pytorch and a pretrained bert model from huggingface.
My train loop:
best_valid_loss = float('inf')
train_losses=[]
valid_losses=[]

for epoch in range(params['epochs']):
     
      print('\n Epoch {:} / {:}'.format(epoch + 1, params['epochs']))
      
      #train model
      train_loss = train(scheduler, optimizer)
      
      #evaluate model
      valid_loss = evaluate()
      
      #save the best model
      if valid_loss &lt; best_valid_loss:
          best_valid_loss = valid_loss
          torch.save(model.state_dict(), model_file)
      
      # append training and validation loss
      train_losses.append(train_loss)
      valid_losses.append(valid_loss)
      
      print(f'\nTraining Loss: {train_loss:.3f}')
      print(f'Validation Loss: {valid_loss:.3f}')


If you want to skip n elements of, for example, some list, you can do this using enumerate:
n = 50000

for i,epoch in enumerate(some_list):
     if i%n == 0:
     
           print('\n Epoch {:} / {:}'.format(epoch + 1, params['epochs']))
           ...

  

But in your case, you can use only an additional condition:
n = 50000

for epoch in range(params['epochs']):
     #train model
     train_loss = train(scheduler, optimizer)
     
     #evaluate model
     valid_loss = evaluate()
  
     #save the best model
     if valid_loss &lt; best_valid_loss:
           best_valid_loss = valid_loss
           torch.save(model.state_dict(), model_file)
  
     # append training and validation loss
     train_losses.append(train_loss)
     valid_losses.append(valid_loss)
  
     ##
     # print every n cycles 
     if epoch%n == 0:
           print('\n Epoch {:} / {:}'.format(epoch + 1, params['epochs']))

           print(f'\nTraining Loss: {train_loss:.3f}')
           print(f'Validation Loss: {valid_loss:.3f}')

