Search code examples
pythonneural-networkpytorchsmoothing

How can I convert SmoothedValue into float for plotting using pyplot?


I am trying to plot the loss vs epoch graph from my neural network using matplotlib.pyplot, but I am encountering a problem. After each epoch, the losses are gathered in an array and are SmoothedValue objects (from utils.py). Right now I am trying to get the loss after each epoch and storing it in an array so that later I can use it as an axis for pyplot. I do this the following way:

for epoch in range(num_epochs):
    epoch_list.append(epoch)
    # train for one epoch, printing every 10 iterations
    metric_logger = train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)

    loss_list = metric_logger.meters.get('loss')
    loss_axis.append(loss_list)

    print("Loss: ", loss_list.value)
    print("Loss: ", loss_list.deque)
    # update the learning rate
    lr_scheduler.step()
    # evaluate on the test dataset
    coco_evaluator = evaluate(model, data_loader_test, device=device)

After training in each epoch, I get the value corresponding to the key 'loss' in the dictionary metric_logger.meters, which I then append to my array loss_axis. However, the elements in metric_logger.meters are of the type SmoothedValue, which then my program can't interpret to plot it. How can I convert this Smoothed Value type to float so that I can plot it?


Solution

  • It should work if you replace loss_axis.append(loss_list) with loss_axis.append(loss_list.value).