0
$\begingroup$

I have the following training method and I'm confused how may I modify the code to plot a training and validation curve history graph with matplotlib

def train(n_epochs, loaders, model, optimizer, criterion, use_cuda, save_path):
    """returns trained model"""
    since = time.time()

    # initialize tracker for minimum validation loss
    valid_loss_min = np.Inf
    train_loss_min = np.Inf

    for epoch in range(1, n_epochs + 1):
        print('\n')
        print('Running Epoch ', epoch, ' of ', n_epochs)
        print('\n')
        time_elapsed = time.time() - since
        print('Training for {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
        # initialize variables to monitor training and validation loss
        train_loss = 0.0
        valid_loss = 0.0

        # train the model 
        model.train()
        for batch_idx, (data, target) in enumerate(loaders['train']):
            # move to GPU
            if use_cuda:
                data, target = data.cuda(), target.cuda()
            ## find the loss and update the model parameters accordingly
            optimizer.zero_grad()
            output = model(data)
            loss = criterion(output, target)
            loss.backward()
            optimizer.step()
            ## record the average training loss
            train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))
            if batch_idx % 10 == 0:  
                    print('Epoch ', epoch, ' Training batch ', batch_idx)
                    print('Train Loss ', train_loss)

        # validate the model 
        model.eval()
        print('\n')
        print('             Evaluating the model')
        print('\n')

        for batch_idx, (data, target) in enumerate(loaders['valid']):
            # move to GPU
            if use_cuda:
                data, target = data.cuda(), target.cuda()
            ## update the average validation loss
            output = model(data)
            loss = criterion(output, target)
            valid_loss = valid_loss + ((1 / (batch_idx + 1)) * (loss.data - valid_loss))
            print('Valid Loss ', valid_loss)
        # print training/validation statistics
        print('\n')
        print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
            epoch,
            train_loss,
            valid_loss
        ))
        print('\n')
        ## save the model if validation loss has decreased
        if valid_loss <= valid_loss_min:
            print('Validation loss decreased ({:.6f} --> {:.6f}). Saving model..'.format(valid_loss_min, valid_loss))
            torch.save(model.state_dict(), save_path)
            print('Model Saved')
            valid_loss_min = valid_loss
    
    time_elapsed = time.time() - since
    print('Training completed in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
    
    return model
$\endgroup$
2
  • 2
    $\begingroup$ The usual way would be to save the loss per batch in some array, and then plot with matplotlib. Can you specify your question? $\endgroup$
    – N. Kiefer
    Apr 21 at 7:45
  • 1
    $\begingroup$ What have you tried? What are the specific problems you have faced? $\endgroup$
    – noe
    Apr 21 at 7:54
1
$\begingroup$

You should use Tensorboard. It has been integrated with PyTorch. See this.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.