from fastai.vision import *
from fastai.gen_doc.nbdoc import *
from fastai.callbacks import *
show_doc(CSVLogger)
class CSVLogger[source][test]
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)
| 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.
show_doc(CSVLogger.read_logged_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 |
You don't call these yourself - they're called by fastai's Callback system automatically to enable the class's functionality.
show_doc(CSVLogger.on_train_begin)
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.
show_doc(CSVLogger.on_epoch_end)
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.
show_doc(CSVLogger.on_train_end)
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.