Skip to content

Instantly share code, notes, and snippets.

@stared
Last active February 25, 2023 10:20
Show Gist options
  • Save stared/dfb4dfaf6d9a8501cd1cc8b8cb806d2e to your computer and use it in GitHub Desktop.
Save stared/dfb4dfaf6d9a8501cd1cc8b8cb806d2e to your computer and use it in GitHub Desktop.
Live loss plot for training models in Keras (see: https://github.com/stared/livelossplot/ for a library)
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kav
Copy link

kav commented Dec 11, 2017

Here's a version with loss and accuracy:

class PlotLearning(Callback):
    def on_train_begin(self, logs={}):
        self.i = 0
        self.x = []
        self.losses = []
        self.val_losses = []
        self.acc = []
        self.val_acc = []
        self.fig = plt.figure()
        
        self.logs = []

    def on_epoch_end(self, epoch, logs={}):
        
        self.logs.append(logs)
        self.x.append(self.i)
        self.losses.append(logs.get('loss'))
        self.val_losses.append(logs.get('val_loss'))
        self.acc.append(logs.get('acc'))
        self.val_acc.append(logs.get('val_acc'))
        self.i += 1
        f, (ax1, ax2) = plt.subplots(1, 2, sharex=True)
        
        clear_output(wait=True)
        
        ax1.set_yscale('log')
        ax1.plot(self.x, self.losses, label="loss")
        ax1.plot(self.x, self.val_losses, label="val_loss")
        ax1.legend()
        
        ax2.plot(self.x, self.acc, label="accuracy")
        ax2.plot(self.x, self.val_acc, label="validation accuracy")
        ax2.legend()
        
        plt.show();
        
plot = PlotLearning()

I also switched loss to a log plot since it tends to resolve that way

@maxberggren
Copy link

Works great, thanks!

@baristahell
Copy link

Thanks for this, it's really nice!

Do you have a way to change the figure size? I'd like it to be larger but something like figsize=(20,10) doesn't work.

@stared
Copy link
Author

stared commented Mar 9, 2018

@stared
Copy link
Author

stared commented Mar 18, 2018

UPDATE: I created a dedicated library: https://github.com/stared/livelossplot/ - contributions are welcomed! :)

You can install it with pip:

pip install livelossplot

@ckolluru
Copy link

ckolluru commented May 1, 2018

Hi, I'd like to know if it is possible to plot loss curves with respect to iteration number in Keras? Thanks!

@ChanChar
Copy link

@ckolluru you can create the above using your own custom callback but in terms of granularity, it looks like Keras supports down to at most a batch level.

@svshivapuja
Copy link

how do we add our own model to this?

@socca
Copy link

socca commented Jul 18, 2018

Thanks for the great project! I was wondering why is my figure show blocking the training? it seems I should close the figure every iteration to let it run and show the updated results. The training does not continue unless I close the figure.

@alexcpn
Copy link

alexcpn commented Jan 22, 2019

Thanks a lot

@rameshKrSah
Copy link

rameshKrSah commented Feb 18, 2019

Thanks!

@vikeshsingh37
Copy link

Thanks!

@Tez01
Copy link

Tez01 commented Mar 17, 2019

Thanks!

@ElaheMrz
Copy link

Thanks for the great project! I was wondering why is my figure show blocking the training? it seems I should close the figure every iteration to let it run and show the updated results. The training does not continue unless I close the figure.

was your problem solved? I have the same problem

@stared
Copy link
Author

stared commented Jun 19, 2019

This script is no longer being maintained (for the last year!).
Please use https://github.com/stared/livelossplot/ instead.

@ElaheMrz
Copy link

This script is no longer being maintained (for the last year!).
Please use https://github.com/stared/livelossplot/ instead.

I'm actually using that , but I have the same problem with that. the figure blocks the process and every epoch I have to close it for the build to continue ...

@maajdl
Copy link

maajdl commented Aug 22, 2019

Thanks a lot it saved my day!
I simply made log-log scale to display th loss ... since I hope the loss decreasing by several orders of magnitude !

@macd2
Copy link

macd2 commented Oct 17, 2019

is it possible to get the live plot per batch instead of per epoch?

@Melavous
Copy link

Hi, I have the same problem than @socca. Could somebody help please?

@lvwarren
Copy link

This is just fantastic. Thank you.

@ahmed-ais
Copy link

@ElaheMrz you need to use Jupyter for that.

@CrazyHrodgar
Copy link

Awesome work, it works very well!
Thanks for sharing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment