{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Tensorboard" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai.collab import *\n", "from fastai.gen_doc.nbdoc import *\n", "from fastai.callbacks.tensorboard import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load tensorboard magic command to show tensorboard embed in Jupyter Notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%load_ext tensorboard" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class LearnerTensorboardWriter[source][test]

\n", "\n", "> LearnerTensorboardWriter(**`learn`**:[`Learner`](/basic_train.html#Learner), **`base_dir`**:`Path`, **`name`**:`str`, **`loss_iters`**:`int`=***`25`***, **`hist_iters`**:`int`=***`500`***, **`stats_iters`**:`int`=***`100`***) :: [`LearnerCallback`](/basic_train.html#LearnerCallback)\n", "\n", "
×

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

\n", "\n", "Broadly useful callback for Learners that writes to Tensorboard. Writes model histograms, losses/metrics, and gradient stats. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(LearnerTensorboardWriter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[`LearnerTensorboardWriter`](/callbacks.tensorboard.html#LearnerTensorboardWriter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First let's show an example of use, with a training on the MovieLens sample dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = untar_data(URLs.ML_SAMPLE)\n", "\n", "ratings = pd.read_csv(path/'ratings.csv')\n", "series2cat(ratings, 'userId', 'movieId')\n", "data = CollabDataBunch.from_df(ratings, seed=42)\n", "\n", "learn = collab_learner(data, n_factors=30, y_range = [0, 5.5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Specify log path for tensorboard to read from. Then append callback partial to learner callback functions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "project_id = 'projct1'\n", "tboard_path = Path('data/tensorboard/' + project_id)\n", "learn.callback_fns.append(partial(LearnerTensorboardWriter, \n", " base_dir=tboard_path, \n", " name='run1'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "run tensorboard magic command with logdir parameter. Default port is 6006." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%tensorboard --logdir=$tboard_path --port=6006" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or you can launch the Tensorboard server from shell with `tensorboard --logdir=data/tensorboard/project1 --port=6006` then navigate to http://localhost:6006" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.fit(10)" ] }, { "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", "To initialize constants in the callback. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(LearnerTensorboardWriter.on_train_begin)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_batch_end[source][test]

\n", "\n", "> on_batch_end(**`last_loss`**:`Tensor`, **`iteration`**:`int`, **`train`**:`bool`, **\\*\\*`kwargs`**)\n", "\n", "
×

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

\n", "\n", "Callback function that writes batch end appropriate data to Tensorboard. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(LearnerTensorboardWriter.on_batch_end)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_backward_end[source][test]

\n", "\n", "> on_backward_end(**`iteration`**:`int`, **`train`**:`bool`, **\\*\\*`kwargs`**)\n", "\n", "
×

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

\n", "\n", "Callback function that writes backward end appropriate data to Tensorboard. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(LearnerTensorboardWriter.on_backward_end)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

on_epoch_end[source][test]

\n", "\n", "> on_epoch_end(**`last_metrics`**:`MetricsList`, **`iteration`**:`int`, **\\*\\*`kwargs`**)\n", "\n", "
×

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

\n", "\n", "Callback function that writes epoch end appropriate data to Tensorboard. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(LearnerTensorboardWriter.on_epoch_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 and output logs for tensorboard to read", "title": "callbacks.tensorboard" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }