{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CSV Logger" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai import *\n", "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]

\n", "\n", "> CSVLogger(`learn`:[`Learner`](/basic_train.html#Learner), `filename`:`str`=`'history'`) :: [`LearnerCallback`](/basic_train.html#LearnerCallback)\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": [ { "name": "stdout", "output_type": "stream", "text": [ "Total time: 00:01\n", "epoch train_loss valid_loss accuracy error_rate\n", "1 0.677579 0.630915 0.929900 0.070100 (00:00)\n", "2 0.596842 0.341453 0.928469 0.071531 (00:00)\n", "3 0.470232 0.203056 0.938484 0.061516 (00:00)\n", "\n" ] } ], "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/ubuntu/.fastai/data/mnist_tiny/train'),\n", " PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/test'),\n", " PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/labels.csv'),\n", " PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/valid'),\n", " PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/history.csv'),\n", " PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/models')]" ] }, "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]

\n", "\n", "> read_logged_file()\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", "
epochtrain_lossvalid_lossaccuracyerror_rate
012.2863652.2167620.5050070.494993
122.1689811.8674150.5050070.494993
232.0282381.7436070.5050070.494993
\n", "
" ], "text/plain": [ " epoch train_loss valid_loss accuracy error_rate\n", "0 1 2.286365 2.216762 0.505007 0.494993\n", "1 2 2.168981 1.867415 0.505007 0.494993\n", "2 3 2.028238 1.743607 0.505007 0.494993" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "learn.csv_logger.read_logged_file()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Undocumented Methods - Methods moved below this line will intentionally be hidden" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

on_train_end[source]

\n", "\n", "> on_train_end(`kwargs`:`Any`)\n", "\n", "Useful for cleaning up things and saving files/models. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(CSVLogger.on_train_end)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

on_epoch_end[source]

\n", "\n", "> on_epoch_end(`epoch`:`int`, `smooth_loss`:`Tensor`, `last_metrics`:`MetricsList`, `kwargs`:`Any`) → `bool`\n", "\n", "Called at the end of an epoch. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(CSVLogger.on_epoch_end)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

on_train_begin[source]

\n", "\n", "> on_train_begin(`metrics_names`:`StrList`, `kwargs`:`Any`)\n", "\n", "To initialize constants in the callback. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(CSVLogger.on_train_begin)" ] }, { "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 }