How to construct plot for loss

I am trying to record the loss over 5 epochs, I have tried to store the value of the loss variable in a list for plotting but I get an error. This is weird to me because I am able to print the value of the loss variable after each step but I cannot append those results into a new list.

Here is my training cell:

encoder.train()
decoder.train()
loss1 = []
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, captions, lengths) in enumerate(train_loader):

    # Set mini-batch dataset
    images = images.to(device)
    captions = captions.to(device)

    # Packed as well as we'll compare to the decoder outputs
    targets = pack_padded_sequence(captions, lengths, batch_first=True)[0]

    # Forward, backward and optimize
    features = encoder(images)
    outputs = decoder(features, captions, lengths)

    loss = criterion(outputs, targets)
    
    # Zero gradients for both networks
    decoder.zero_grad()
    encoder.zero_grad()

    loss.backward()
    optimizer.step()

    # Print log info
    if i % log_step == 0:
        print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
              .format(epoch, num_epochs, i, total_step, loss.item())) 

   
loss1.append(loss.item)

The last loss1.append(loss.item) should be loss1.append(loss.item()).

1 Like