Callbacks that saves the tracked metrics during training

CSV Logger

class CSVLogger[source]

CSVLogger(`learn`:Learner, `filename`:str=`'history'`) :: LearnerCallback

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.

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

epoch train_loss valid_loss accuracy error_rate
1 0.586522 0.433411 0.935622 0.064378
2 0.448884 0.187622 0.949928 0.050072
3 0.345840 0.130823 0.957082 0.042918

Training details have been saved in 'history.csv'.

learn.path.ls()
[PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/labels.csv'),
 PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/export.pkl'),
 PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/test'),
 PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/train'),
 PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/history.csv'),
 PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/models'),
 PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/cleaned.csv'),
 PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/valid')]

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]

read_logged_file()

Read the content of saved file

learn.csv_logger.read_logged_file()
epoch train_loss valid_loss accuracy error_rate
0 1 0.586522 0.433411 0.935622 0.064378
1 2 0.448884 0.187622 0.949928 0.050072
2 3 0.345840 0.130823 0.957082 0.042918

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]

on_train_begin(`kwargs`:Any)

Prepare file with metric names.

on_epoch_end[source]

on_epoch_end(`epoch`:int, `smooth_loss`:Tensor, `last_metrics`:MetricsList, `kwargs`:Any) → bool

Add a line with epoch number, smooth_loss and last_metrics.

on_train_end[source]

on_train_end(`kwargs`:Any)

Close the file.