{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Model Layers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This module contains many layer classes that we might be interested in using in our models. These layers complement the default [Pytorch layers](https://pytorch.org/docs/stable/nn.html) which we can also use as predefined layers." ] }, { "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 *" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "
class AdaptiveConcatPool2d[source]AdaptiveConcatPool2d(`sz`:`Optional`\\[`int`\\]=`None`) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)"
],
"text/plain": [
"class Lambda[source]Lambda(`func`:`LambdaFunc`) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)"
],
"text/plain": [
"Flatten[source]Flatten() → `Tensor`\n",
"\n",
"Flattens `x` to a single dimension, often used at the end of a model. "
],
"text/plain": [
"PoolFlatten[source]PoolFlatten() → [`Sequential`](https://pytorch.org/docs/stable/nn.html#torch.nn.Sequential)\n",
"\n",
"Apply [`nn.AdaptiveAvgPool2d`](https://pytorch.org/docs/stable/nn.html#torch.nn.AdaptiveAvgPool2d) to `x` and then flatten the result. "
],
"text/plain": [
"ResizeBatch[source]ResizeBatch(`size`:`int`) → `Tensor`\n",
"\n",
"Layer that resizes x to `size`, good for connecting mismatched layers. "
],
"text/plain": [
"class CrossEntropyFlat[source]CrossEntropyFlat(`weight`=`None`, `size_average`=`None`, `ignore_index`=`-100`, `reduce`=`None`, `reduction`=`'elementwise_mean'`) :: [`CrossEntropyLoss`](https://pytorch.org/docs/stable/nn.html#torch.nn.CrossEntropyLoss)"
],
"text/plain": [
"class MSELossFlat[source]MSELossFlat(`size_average`=`None`, `reduce`=`None`, `reduction`=`'elementwise_mean'`) :: [`MSELoss`](https://pytorch.org/docs/stable/nn.html#torch.nn.MSELoss)\n",
"\n",
"Same as [`nn.MSELoss`](https://pytorch.org/docs/stable/nn.html#torch.nn.MSELoss), but flattens input and target. "
],
"text/plain": [
"class Debugger[source]Debugger() :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n",
"\n",
"A module to debug inside a model. "
],
"text/plain": [
"class NoopLoss[source]NoopLoss() :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n",
"\n",
"Just returns the `output`. "
],
"text/plain": [
"class PixelShuffle_ICNR[source]PixelShuffle_ICNR(`ni`:`int`, `nf`:`int`=`None`, `scale`:`int`=`2`) :: [`Sequential`](https://pytorch.org/docs/stable/nn.html#torch.nn.Sequential)\n",
"\n",
"Upsample by `scale` from `ni` filters to `nf` (default `ni`), using [`nn.PixelShuffle`](https://pytorch.org/docs/stable/nn.html#torch.nn.PixelShuffle), [`icnr`](/layers.html#icnr) init, and [`weight_norm`](https://pytorch.org/docs/stable/nn.html#torch.nn.weight_norm). "
],
"text/plain": [
"bn_drop_lin[source]bn_drop_lin(`n_in`:`int`, `n_out`:`int`, `bn`:`bool`=`True`, `p`:`float`=`0.0`, `actn`:`Optional`\\[[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\\]=`None`)"
],
"text/plain": [
"conv2d[source]conv2d(`ni`:`int`, `nf`:`int`, `ks`:`int`=`3`, `stride`:`int`=`1`, `padding`:`int`=`None`, `bias`=`False`) → [`Conv2d`](https://pytorch.org/docs/stable/nn.html#torch.nn.Conv2d)\n",
"\n",
"Create [`nn.Conv2d`](https://pytorch.org/docs/stable/nn.html#torch.nn.Conv2d) layer: `ni` inputs, `nf` outputs, `ks` kernel size. `padding` defaults to `k//2`. "
],
"text/plain": [
"conv2d_trans[source]conv2d_trans(`ni`:`int`, `nf`:`int`, `ks`:`int`=`2`, `stride`:`int`=`2`, `padding`:`int`=`0`, `bias`=`False`) → [`ConvTranspose2d`](https://pytorch.org/docs/stable/nn.html#torch.nn.ConvTranspose2d)\n",
"\n",
"Create [`nn.ConvTranspose2d`](https://pytorch.org/docs/stable/nn.html#torch.nn.ConvTranspose2d) layer: `ni` inputs, `nf` outputs, `ks` kernel size, `stride`: stride. `padding` defaults to 0. "
],
"text/plain": [
"conv_layer[source]conv_layer(`ni`:`int`, `nf`:`int`, `ks`:`int`=`3`, `stride`:`int`=`1`, `padding`:`int`=`None`, `bias`:`bool`=`False`, `bn`:`bool`=`True`, `leaky`:`float`=`None`, `transpose`:`bool`=`False`)"
],
"text/plain": [
"embedding[source]embedding(`ni`:`int`, `nf`:`int`) → [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)"
],
"text/plain": [
"simple_cnn[source]simple_cnn(`actns`:`Collection`\\[`int`\\], `kernel_szs`:`Collection`\\[`int`\\]=`None`, `strides`:`Collection`\\[`int`\\]=`None`, `bn`=`False`) → [`Sequential`](https://pytorch.org/docs/stable/nn.html#torch.nn.Sequential)\n",
"\n",
"CNN with [`conv_layer`](/layers.html#conv_layer) defined by `actns`, `kernel_szs` and `strides`, plus batchnorm if `bn`. "
],
"text/plain": [
"std_upsample_head[source]std_upsample_head(`c`, `nfs`:`Collection`\\[`int`\\]) → [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)"
],
"text/plain": [
"trunc_normal_[source]trunc_normal_(`x`:`Tensor`, `mean`:`float`=`0.0`, `std`:`float`=`1.0`) → `Tensor`\n",
"\n",
"Truncated normal initialization. "
],
"text/plain": [
"icnr[source]icnr(`x`, `scale`=`2`, `init`=`'kaiming_normal_'`)\n",
"\n",
"ICNR init. "
],
"text/plain": [
"forward[source]forward(`x`:`Tensor`) → `Tensor`\n",
"\n",
"Defines the computation performed at every call. Should be overridden by all subclasses.\n",
"\n",
".. note::\n",
" Although the recipe for forward pass needs to be defined within\n",
" this function, one should call the :class:`Module` instance afterwards\n",
" instead of this since the former takes care of running the\n",
" registered hooks while the latter silently ignores them. "
],
"text/plain": [
"forward[source]forward(`input`:`Tensor`, `target`:`Tensor`) → `Rank0Tensor`\n",
"\n",
"Defines the computation performed at every call. Should be overridden by all subclasses.\n",
"\n",
".. note::\n",
" Although the recipe for forward pass needs to be defined within\n",
" this function, one should call the :class:`Module` instance afterwards\n",
" instead of this since the former takes care of running the\n",
" registered hooks while the latter silently ignores them. "
],
"text/plain": [
"forward[source]forward(`input`:`Tensor`, `target`:`Tensor`) → `Rank0Tensor`\n",
"\n",
"Defines the computation performed at every call. Should be overridden by all subclasses.\n",
"\n",
".. note::\n",
" Although the recipe for forward pass needs to be defined within\n",
" this function, one should call the :class:`Module` instance afterwards\n",
" instead of this since the former takes care of running the\n",
" registered hooks while the latter silently ignores them. "
],
"text/plain": [
"forward[source]forward(`x`)\n",
"\n",
"Defines the computation performed at every call. Should be overridden by all subclasses.\n",
"\n",
".. note::\n",
" Although the recipe for forward pass needs to be defined within\n",
" this function, one should call the :class:`Module` instance afterwards\n",
" instead of this since the former takes care of running the\n",
" registered hooks while the latter silently ignores them. "
],
"text/plain": [
"forward[source]forward(`x`)\n",
"\n",
"Defines the computation performed at every call. Should be overridden by all subclasses.\n",
"\n",
".. note::\n",
" Although the recipe for forward pass needs to be defined within\n",
" this function, one should call the :class:`Module` instance afterwards\n",
" instead of this since the former takes care of running the\n",
" registered hooks while the latter silently ignores them. "
],
"text/plain": [
"forward[source]forward(`output`, `target`)\n",
"\n",
"Defines the computation performed at every call. Should be overridden by all subclasses.\n",
"\n",
".. note::\n",
" Although the recipe for forward pass needs to be defined within\n",
" this function, one should call the :class:`Module` instance afterwards\n",
" instead of this since the former takes care of running the\n",
" registered hooks while the latter silently ignores them. "
],
"text/plain": [
"class NoopLoss[source]NoopLoss() :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n",
"\n",
"Just returns the `output`. "
],
"text/plain": [
"class PixelShuffle_ICNR[source]PixelShuffle_ICNR(`ni`:`int`, `nf`:`int`=`None`, `scale`:`int`=`2`) :: [`Sequential`](https://pytorch.org/docs/stable/nn.html#torch.nn.Sequential)\n",
"\n",
"Upsample by `scale` from `ni` filters to `nf` (default `ni`), using [`nn.PixelShuffle`](https://pytorch.org/docs/stable/nn.html#torch.nn.PixelShuffle), [`icnr`](/layers.html#icnr) init, and [`weight_norm`](https://pytorch.org/docs/stable/nn.html#torch.nn.weight_norm). "
],
"text/plain": [
"icnr[source]icnr(`x`, `scale`=`2`, `init`=`'kaiming_normal_'`)\n",
"\n",
"ICNR init. "
],
"text/plain": [
"