How do I plot accuracy and val loss + training loss after I've trained my model?

I’m on lesson 1 and I’m wondering, after I do learn.fit() and train my model, how do I plot my accuracy and losses over epoch?

1 Like

I figured it out! This is how I was able to do it for anyone who also comes across this problem:

After you train your model (learn) use this:

trainloss = learn.sched.losses
valloss = learn.sched.val_losses
epochs = learn.sched.epochs
mini_batches= int(len(trainloss)/len(valloss))

epochs = [x / mini_batches for x in epochs]

plt.plot(epochs[1:],trainloss[mini_batches::mini_batches])
plt.plot(epochs[1:],valloss[1:])
plt.show()

I get my training losses and validation losses and epochs into lists.

Since my training loss is calculated at each mini-batch and my validation loss is calculated at the end of each full batch (epoch), I need to only plot the training data after each full batch.

Then I just plot every mini_batches’th training loss along with validation loss.

So my data is plotted for every epoch not for every backprop operation.

You can also plot training loss after each mini batch while keeping val loss constant until the epoch is done.

8 Likes

Hi! You don’t have to wait to finish your training. You can use ShowGraphCallback() callback in your training function like this:

learn.fine_tune(epochs=3, cbs=[
    ShowGraphCallback(),
]

I’m using fastai v2.5.2