Training loss and validation loss graph

Hello, am trying to draw graph of training loss and validation loss using matplotlip.pyplot but i usually get black graph.

my code is like this
plt.plot(train_loss, label=‘Training loss’)
plt.plot(valid_loss, label=‘Validation loss’)

and the code which produce those loss value is
n_epochs = 30
valid_loss_min = np.Inf

for epochs in range(1, n_epochs+1):

train_loss = 0.0
valid_loss = 0.0

for images, labels in train_loader:
    output = model(images)
    loss = criterion(output,labels)
    train_loss += loss.item()
for images, labels in valid_loader:
    output = model(images)
    loss = criterion(output,labels)
    valid_loss += loss.item()
train_loss = train_loss/len(train_loader.dataset)
valid_loss = valid_loss/len(valid_loader.dataset)

print('Epoch:  {}. Training Loss: {:.6f}. Validation_loss: {:.6f}'.format(epochs,train_loss,valid_loss))

if valid_loss <= valid_loss_min:
    print( 'Varidation Loss is decrease: ( {:.6f} -->{:.6f}). save the model...'.format(valid_loss_min, valid_loss)), '')
    valid_loss_min = valid_loss
1 Like

it should be list of values. Try creating training_loss list and validation loss list out of the for loop
and append the training and validation loss to it.

Your mistake lies in here
train_loss += loss.item()

correct it with this one for both loops
train_loss += loss.item().len(images)