Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Live loss plot for training models in Keras (see: https://github.com/stared/livelossplot/ for a library)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kav

This comment has been minimized.

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

This comment has been minimized.

Copy link

maxberggren commented Mar 2, 2018

Works great, thanks!

@baristahell

This comment has been minimized.

Copy link

baristahell commented Mar 9, 2018

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

This comment has been minimized.

Copy link
Owner Author

stared commented Mar 9, 2018

@stared

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

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

This comment has been minimized.

Copy link

ChanChar commented May 23, 2018

@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

This comment has been minimized.

Copy link

svshivapuja commented Jun 25, 2018

how do we add our own model to this?

@socca

This comment has been minimized.

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

This comment has been minimized.

Copy link

alexcpn commented Jan 22, 2019

Thanks a lot

@rameshKrSah

This comment has been minimized.

Copy link

rameshKrSah commented Feb 18, 2019

Thanks!

@vikeshsingh37

This comment has been minimized.

Copy link

vikeshsingh37 commented Mar 2, 2019

Thanks!

@Tez01

This comment has been minimized.

Copy link

Tez01 commented Mar 17, 2019

Thanks!

@ElaheMrz

This comment has been minimized.

Copy link

ElaheMrz commented Jun 19, 2019

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

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

Copy link

ElaheMrz commented Jun 19, 2019

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

macd2 commented Oct 17, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.