{ "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": 1, "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": 2, "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 StdUpsample[source]StdUpsample(`n_in`:`int`, `n_out`:`int`) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)"
],
"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": [
"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_relu[source]conv2d_relu(`ni`:`int`, `nf`:`int`, `ks`:`int`=`3`, `stride`:`int`=`1`, `padding`:`int`=`None`, `bn`:`bool`=`False`, `bias`:`bool`=`False`) → [`Sequential`](https://pytorch.org/docs/stable/nn.html#torch.nn.Sequential)"
],
"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`) → [`Sequential`](https://pytorch.org/docs/stable/nn.html#torch.nn.Sequential)"
],
"text/plain": [
"get_embedding[source]get_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 [`conv2d_relu`](/layers.html#conv2d_relu) layers 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": [
"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(`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": [
"