callbacks.csv_logger

Callbacks that saves the tracked metrics during training

CSV Logger

class CSVLogger[source][test]

CSVLogger(learn:Learner, filename:str='history', append:bool=False) :: LearnerCallback Tests found for CSVLogger:

  • pytest -sv tests/test_callbacks_csv_logger.py::test_logger [source]

To run tests please refer to this guide.

A LearnerCallback that saves history of metrics while training learn into CSV filename.

First let’s show an example of use, with a training on the usual MNIST dataset.

  1. path = untar_data(URLs.MNIST_TINY)
  2. data = ImageDataBunch.from_folder(path)
  3. learn = Learner(data, simple_cnn((3, 16, 16, 2)), metrics=[accuracy, error_rate], callback_fns=[CSVLogger])
  1. learn.fit(3)

Total time: 00:02

epochtrain_lossvalid_lossaccuracyerror_ratetime
00.6430730.5361240.9313300.06867000:00
10.5231300.2463200.9527900.04721000:00
20.4027640.1479980.9384840.06151600:00

Training details have been saved in ‘history.csv’.

Note that it only saves float/int metrics, so time currently is not saved. This could be saved but requires changing the recording - you can submit a PR fixing that.

  1. learn.path.ls()
  1. [PosixPath('/home/stas/.fastai/data/mnist_tiny/models'),
  2. PosixPath('/home/stas/.fastai/data/mnist_tiny/labels.csv'),
  3. PosixPath('/home/stas/.fastai/data/mnist_tiny/cleaned.csv'),
  4. PosixPath('/home/stas/.fastai/data/mnist_tiny/train'),
  5. PosixPath('/home/stas/.fastai/data/mnist_tiny/test'),
  6. PosixPath('/home/stas/.fastai/data/mnist_tiny/valid'),
  7. PosixPath('/home/stas/.fastai/data/mnist_tiny/export.pkl'),
  8. PosixPath('/home/stas/.fastai/data/mnist_tiny/history.csv')]

Note that, as with all LearnerCallback, you can access the object as an attribute of learn after it has been created. Here it’s learn.csv_logger.

read_logged_file[source][test]

read_logged_file() Tests found for read_logged_file:

Some other tests where read_logged_file is used:

  • pytest -sv tests/test_callbacks_csv_logger.py::test_logger [source]

To run tests please refer to this guide.

Read the content of saved file

  1. learn.csv_logger.read_logged_file()
epochtrain_lossvalid_lossaccuracyerror_rate
000.6430730.5361240.9313300.068670
110.5231300.2463200.9527900.047210
220.4027640.1479980.9384840.061516

Optionally you can set append=True to log results of consequent stages of training.

  1. # don't forget to remove the old file
  2. if learn.csv_logger.path.exists(): os.remove(learn.csv_logger.path)
  1. learn = Learner(data, simple_cnn((3, 16, 16, 2)), metrics=[accuracy, error_rate],
  2. callback_fns=[partial(CSVLogger, append=True)])
  1. # stage-1
  2. learn.fit(3)

Total time: 00:02

epochtrain_lossvalid_lossaccuracyerror_ratetime
00.6045490.4932410.7668100.23319000:00
10.4863670.2261890.9484980.05150200:00
20.3778470.1271420.9585120.04148800:00
  1. # stage-2
  2. learn.fit(3)

Total time: 00:02

epochtrain_lossvalid_lossaccuracyerror_ratetime
00.1894410.1185320.9542200.04578000:00
10.1774320.1109130.9656650.03433500:00
20.1686260.0976460.9685260.03147400:00
  1. learn.csv_logger.read_logged_file()
epochtrain_lossvalid_lossaccuracyerror_rate
000.6045490.4932410.7668100.233190
110.4863670.2261890.9484980.051502
220.3778470.1271420.9585120.041488
3epochtrain_lossvalid_lossaccuracyerror_rate
400.1894410.1185320.9542200.045780
510.1774320.1109130.9656650.034335
620.1686260.0976460.9685260.031474

Calback methods

You don’t call these yourself - they’re called by fastai’s Callback system automatically to enable the class’s functionality.

on_train_begin[source][test]

on_train_begin(**kwargs:Any) No tests found for on_train_begin. To contribute a test please refer to this guide and this discussion.

Prepare file with metric names.

on_epoch_end[source][test]

on_epoch_end(epoch:int, smooth_loss:Tensor, last_metrics:MetricsList, **kwargs:Any) → bool No tests found for on_epoch_end. To contribute a test please refer to this guide and this discussion.

Add a line with epoch number, smooth_loss and last_metrics.

on_train_end[source][test]

on_train_end(**kwargs:Any) No tests found for on_train_end. To contribute a test please refer to this guide and this discussion.

Close the file.


Company logo

©2021 fast.ai. All rights reserved.
Site last generated: Jan 5, 2021