{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## CSV Logger" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai.vision import *\n", "from fastai.gen_doc.nbdoc import *\n", "from fastai.callbacks import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class CSVLogger[source][test]

\n", "\n", "> CSVLogger(**`learn`**:[`Learner`](/basic_train.html#Learner), **`filename`**:`str`=***`'history'`***, **`append`**:`bool`=***`False`***) :: [`LearnerCallback`](/basic_train.html#LearnerCallback)\n", "\n", "
×

Tests found for CSVLogger:

To run tests please refer to this guide.

\n", "\n", "A [`LearnerCallback`](/basic_train.html#LearnerCallback) that saves history of metrics while training `learn` into CSV `filename`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(CSVLogger)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First let's show an example of use, with a training on the usual MNIST dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = untar_data(URLs.MNIST_TINY)\n", "data = ImageDataBunch.from_folder(path)\n", "learn = Learner(data, simple_cnn((3, 16, 16, 2)), metrics=[accuracy, error_rate], callback_fns=[CSVLogger])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Total time: 00:07

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
epochtrain_lossvalid_lossaccuracyerror_ratetime
00.6156210.4951740.8111590.18884100:02
10.4839510.1999000.9442060.05579400:02
20.3653800.1257060.9570820.04291800:02
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "learn.fit(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Training details have been saved in 'history.csv'." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[PosixPath('/home/turgutluk/.fastai/data/mnist_tiny/test'),\n", " PosixPath('/home/turgutluk/.fastai/data/mnist_tiny/labels.csv'),\n", " PosixPath('/home/turgutluk/.fastai/data/mnist_tiny/train'),\n", " PosixPath('/home/turgutluk/.fastai/data/mnist_tiny/valid'),\n", " PosixPath('/home/turgutluk/.fastai/data/mnist_tiny/models'),\n", " PosixPath('/home/turgutluk/.fastai/data/mnist_tiny/history.csv')]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "learn.path.ls()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that, as with all [`LearnerCallback`](/basic_train.html#LearnerCallback), you can access the object as an attribute of `learn` after it has been created. Here it's `learn.csv_logger`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

read_logged_file[source][test]

\n", "\n", "> read_logged_file()\n", "\n", "
×

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.

\n", "\n", "Read the content of saved file " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(CSVLogger.read_logged_file)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
epochtrain_lossvalid_lossaccuracyerror_ratetime
000.6156210.4951740.8111590.188841NaN
110.4839510.1999000.9442060.055794NaN
220.3653800.1257060.9570820.042918NaN
\n", "
" ], "text/plain": [ " epoch train_loss valid_loss accuracy error_rate time\n", "0 0 0.615621 0.495174 0.811159 0.188841 NaN\n", "1 1 0.483951 0.199900 0.944206 0.055794 NaN\n", "2 2 0.365380 0.125706 0.957082 0.042918 NaN" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "learn.csv_logger.read_logged_file()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Optionally you can set `append=True` to log results of consequent stages of training." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# don't forget to remove the old file\n", "if learn.csv_logger.path.exists(): os.remove(learn.csv_logger.path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn = Learner(data, simple_cnn((3, 16, 16, 2)), metrics=[accuracy, error_rate],\n", " callback_fns=[partial(CSVLogger, append=True)])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Total time: 00:07

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
epochtrain_lossvalid_lossaccuracyerror_ratetime
00.6771570.6258890.8726750.12732500:02
10.5966460.3307010.9556510.04434900:02
20.4565790.1362930.9570820.04291800:02
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# stage-1\n", "learn.fit(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Total time: 00:07

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
epochtrain_lossvalid_lossaccuracyerror_ratetime
00.1991190.1290350.9484980.05150200:02
10.1968800.1144800.9628040.03719600:02
20.1842170.1154390.9585120.04148800:02
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# stage-2\n", "learn.fit(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "

\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
epochtrain_lossvalid_lossaccuracyerror_ratetime
000.6771570.6258890.8726750.127325NaN
110.5966460.3307010.9556510.044349NaN
220.4565790.1362930.9570820.042918NaN
3epochtrain_lossvalid_lossaccuracyerror_ratetime
400.1991190.1290350.9484980.051502NaN
510.1968800.1144800.9628040.037196NaN
620.1842170.1154390.9585120.041488NaN
\n", "
" ], "text/plain": [ " epoch train_loss valid_loss accuracy error_rate time\n", "0 0 0.677157 0.625889 0.872675 0.127325 NaN\n", "1 1 0.596646 0.330701 0.955651 0.044349 NaN\n", "2 2 0.456579 0.136293 0.957082 0.042918 NaN\n", "3 epoch train_loss valid_loss accuracy error_rate time\n", "4 0 0.199119 0.129035 0.948498 0.051502 NaN\n", "5 1 0.196880 0.114480 0.962804 0.037196 NaN\n", "6 2 0.184217 0.115439 0.958512 0.041488 NaN" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "learn.csv_logger.read_logged_file()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Calback methods" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You don't call these yourself - they're called by fastai's [`Callback`](/callback.html#Callback) system automatically to enable the class's functionality." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_train_begin[source][test]

\n", "\n", "> on_train_begin(**\\*\\*`kwargs`**:`Any`)\n", "\n", "
×

No tests found for on_train_begin. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Prepare file with metric names. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(CSVLogger.on_train_begin)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_epoch_end[source][test]

\n", "\n", "> on_epoch_end(**`epoch`**:`int`, **`smooth_loss`**:`Tensor`, **`last_metrics`**:`MetricsList`, **\\*\\*`kwargs`**:`Any`) → `bool`\n", "\n", "
×

No tests found for on_epoch_end. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Add a line with `epoch` number, `smooth_loss` and `last_metrics`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(CSVLogger.on_epoch_end)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_train_end[source][test]

\n", "\n", "> on_train_end(**\\*\\*`kwargs`**:`Any`)\n", "\n", "
×

No tests found for on_train_end. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Close the file. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(CSVLogger.on_train_end)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Undocumented Methods - Methods moved below this line will intentionally be hidden" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## New Methods - Please document or move to the undocumented section" ] } ], "metadata": { "jekyll": { "keywords": "fastai", "summary": "Callbacks that saves the tracked metrics during training", "title": "callbacks.csv_logger" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }