{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# default_exp callback.core" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "from fastai2.data.all import *\n", "from fastai2.optimizer import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from nbdev.showdoc import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "_all_ = ['CancelFitException', 'CancelEpochException', 'CancelTrainException', 'CancelValidException', 'CancelBatchException']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Callback\n", "\n", "> Basic callbacks for Learner" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Events" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Callbacks can occur at any of these times:: *begin_fit begin_epoch begin_train begin_batch after_pred after_loss after_backward after_step after_cancel_batch after_batch after_cancel_train after_train begin_validate after_cancel_validate after_validate after_cancel_epoch after_epoch after_cancel_fit after_fit*." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# export\n", "_events = L.split('begin_fit begin_epoch begin_train begin_batch after_pred after_loss \\\n", " after_backward after_step after_cancel_batch after_batch after_cancel_train \\\n", " after_train begin_validate after_cancel_validate after_validate after_cancel_epoch \\\n", " after_epoch after_cancel_fit after_fit')\n", "\n", "mk_class('event', **_events.map_dict(),\n", " doc=\"All possible events as attributes to get tab-completion and typo-proofing\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# export\n", "_all_ = ['event']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "
class event[source]event(**\\*`args`**, **\\*\\*`kwargs`**)\n",
"\n",
"All possible events as attributes to get tab-completion and typo-proofing"
],
"text/plain": [
"Callback.__call__[source]Callback.__call__(**`event_name`**)\n",
"\n",
"Call `self.{event_name}` if it's defined"
],
"text/plain": [
"GetAttr.__getattr__[source]GetAttr.__getattr__(**`k`**)\n",
"\n"
],
"text/plain": [
"Callback.name[source]class TrainEvalCallback[source]TrainEvalCallback(**`begin_fit`**=*`None`*, **`begin_epoch`**=*`None`*, **`begin_train`**=*`None`*, **`begin_batch`**=*`None`*, **`after_pred`**=*`None`*, **`after_loss`**=*`None`*, **`after_backward`**=*`None`*, **`after_step`**=*`None`*, **`after_cancel_batch`**=*`None`*, **`after_batch`**=*`None`*, **`after_cancel_train`**=*`None`*, **`after_train`**=*`None`*, **`begin_validate`**=*`None`*, **`after_cancel_validate`**=*`None`*, **`after_validate`**=*`None`*, **`after_cancel_epoch`**=*`None`*, **`after_epoch`**=*`None`*, **`after_cancel_fit`**=*`None`*, **`after_fit`**=*`None`*) :: [`Callback`](callback.core#Callback)\n",
"\n",
"[`Callback`](callback.core#Callback) that tracks the number of iterations done and properly sets training/eval mode"
],
"text/plain": [
"TrainEvalCallback.begin_fit[source]TrainEvalCallback.begin_fit()\n",
"\n",
"Set the iter and epoch counters to 0, put the model and the right device"
],
"text/plain": [
"TrainEvalCallback.after_batch[source]TrainEvalCallback.after_batch()\n",
"\n",
"Update the iter counter (in training mode)"
],
"text/plain": [
"TrainEvalCallback.begin_train[source]TrainEvalCallback.begin_train()\n",
"\n",
"Set the model in training mode"
],
"text/plain": [
"TrainEvalCallback.begin_validate[source]TrainEvalCallback.begin_validate()\n",
"\n",
"Set the model in validation mode"
],
"text/plain": [
"class GatherPredsCallback[source]GatherPredsCallback(**`with_input`**=*`False`*, **`with_loss`**=*`False`*, **`save_preds`**=*`None`*, **`save_targs`**=*`None`*, **`concat_dim`**=*`0`*) :: [`Callback`](callback.core#Callback)\n",
"\n",
"[`Callback`](callback.core#Callback) that saves the predictions and targets, optionally `with_loss`"
],
"text/plain": [
"GatherPredsCallback.begin_validate[source]GatherPredsCallback.begin_validate()\n",
"\n",
"Initialize containers"
],
"text/plain": [
"GatherPredsCallback.after_batch[source]GatherPredsCallback.after_batch()\n",
"\n",
"Save predictions, targets and potentially losses"
],
"text/plain": [
"GatherPredsCallback.after_validate[source]GatherPredsCallback.after_validate()\n",
"\n",
"Concatenate all recorded tensors"
],
"text/plain": [
"class FetchPredsCallback[source]FetchPredsCallback(**`ds_idx`**=*`1`*, **`dl`**=*`None`*, **`with_input`**=*`False`*, **`with_decoded`**=*`False`*, **`cbs`**=*`None`*) :: [`Callback`](callback.core#Callback)\n",
"\n",
"A callback to fetch predictions during the training loop"
],
"text/plain": [
"class CancelBatchException[source]CancelBatchException(**\\*`args`**, **\\*\\*`kwargs`**) :: `Exception`\n",
"\n",
"Interrupts training and go to `after_fit`"
],
"text/plain": [
"class CancelTrainException[source]CancelTrainException(**\\*`args`**, **\\*\\*`kwargs`**) :: `Exception`\n",
"\n",
"Skip the rest of the validation part of the epoch and go to `after_validate`"
],
"text/plain": [
"class CancelValidException[source]CancelValidException(**\\*`args`**, **\\*\\*`kwargs`**) :: `Exception`\n",
"\n",
"Skip the rest of this epoch and go to `after_epoch`"
],
"text/plain": [
"class CancelEpochException[source]CancelEpochException(**\\*`args`**, **\\*\\*`kwargs`**) :: `Exception`\n",
"\n",
"Skip the rest of the training part of the epoch and go to `after_train`"
],
"text/plain": [
"class CancelFitException[source]CancelFitException(**\\*`args`**, **\\*\\*`kwargs`**) :: `Exception`\n",
"\n",
"Skip the rest of this batch and go to `after_batch`"
],
"text/plain": [
"