{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Torch Core" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This module contains all the basic functions we need in other modules of the fastai library (split with [`core`](/core.html#core) that contains the ones not requiring pytorch). Its documentation can easily be skipped at a first read, unless you want to know what a given function does." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai.imports import *\n", "from fastai.gen_doc.nbdoc import *\n", "from fastai.layers import *\n", "from fastai.torch_core import * " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Global constants" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`AdamW = partial(optim.Adam, betas=(0.9,0.99))`
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`bn_types = (nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d)` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`defaults.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you are trying to make fastai run on the CPU, simply change the default device: `defaults.device = torch.device('cpu')`. \n", "\n", "Alternatively, if not using wildcard imports: `fastai.torch_core.defaults.device = torch.device('cpu')`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions that operate conversions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "batch_to_half
[source][test]batch_to_half
(**`b`**:`Collection`\\[`Tensor`\\]) → `Collection`\\[`Tensor`\\]\n",
"\n",
"flatten_model
[source]-pytest\" style=\"float:right; padding-right:10px\">[test]flatten_model
(**`m`**)\n",
"\n",
"No tests found for <lambda>
. To contribute a test please refer to this guide and this discussion.
model2half
[source][test]model2half
(**`model`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)) → [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n",
"\n",
"np2model_tensor
[source][test]np2model_tensor
(**`a`**)\n",
"\n",
"requires_grad
[source][test]requires_grad
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`b`**:`Optional`\\[`bool`\\]=***`None`***) → `Optional`\\[`bool`\\]\n",
"\n",
"Tests found for requires_grad
:
pytest -sv tests/test_torch_core.py::test_requires_grad
[source]pytest -sv tests/test_torch_core.py::test_requires_grad_set
[source]Some other tests where requires_grad
is used:
pytest -sv tests/test_torch_core.py::test_set_bn_eval
[source]To run tests please refer to this guide.
tensor
[source][test]tensor
(**`x`**:`Any`, **\\*`rest`**) → `Tensor`\n",
"\n",
"Tests found for tensor
:
pytest -sv tests/test_torch_core.py::test_tensor_with_list
[source]pytest -sv tests/test_torch_core.py::test_tensor_with_ndarray
[source]pytest -sv tests/test_torch_core.py::test_tensor_with_tensor
[source]Some other tests where tensor
is used:
pytest -sv tests/test_torch_core.py::test_to_cpu
[source]pytest -sv tests/test_torch_core.py::test_to_data
[source]pytest -sv tests/test_torch_core.py::test_to_detach
[source]pytest -sv tests/test_torch_core.py::test_to_float
[source]pytest -sv tests/test_torch_core.py::test_to_half
[source]To run tests please refer to this guide.
to_cpu
[source][test]to_cpu
(**`b`**:`ItemsList`)\n",
"\n",
"to_data
[source][test]to_data
(**`b`**:`ItemsList`)\n",
"\n",
"to_detach
[source][test]to_detach
(**`b`**:`Tensors`, **`cpu`**:`bool`=***`True`***)\n",
"\n",
"to_device
[source][test]to_device
(**`b`**:`Tensors`, **`device`**:[`device`](https://pytorch.org/docs/stable/tensor_attributes.html#torch-device))\n",
"\n",
"No tests found for to_device
. To contribute a test please refer to this guide and this discussion.
to_half
[source][test]to_half
(**`b`**:`Collection`\\[`Tensor`\\]) → `Collection`\\[`Tensor`\\]\n",
"\n",
"to_np
[source][test]to_np
(**`x`**)\n",
"\n",
"try_int
[source][test]try_int
(**`o`**:`Any`) → `Any`\n",
"\n",
"No tests found for try_int
. To contribute a test please refer to this guide and this discussion.
apply_init
[source][test]apply_init
(**`m`**, **`init_func`**:`LayerFunc`)\n",
"\n",
"apply_leaf
[source][test]apply_leaf
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`f`**:`LayerFunc`)\n",
"\n",
"cond_init
[source][test]cond_init
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`init_func`**:`LayerFunc`)\n",
"\n",
"No tests found for cond_init
. To contribute a test please refer to this guide and this discussion.
in_channels
[source][test]in_channels
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)) → `List`\\[`int`\\]\n",
"\n",
"init_default
[source][test]init_default
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`func`**:`LayerFunc`=***`'kaiming_normal_'`***) → [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n",
"\n",
"No tests found for init_default
. To contribute a test please refer to this guide and this discussion.
children
[source][test]children
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)) → `ModuleList`\n",
"\n",
"children_and_parameters
[source][test]children_and_parameters
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module))\n",
"\n",
"No tests found for children_and_parameters
. To contribute a test please refer to this guide and this discussion.
first_layer
[source][test]first_layer
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)) → [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n",
"\n",
"last_layer
[source][test]last_layer
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)) → [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n",
"\n",
"num_children
[source][test]num_children
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)) → `int`\n",
"\n",
"one_param
[source][test]one_param
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)) → `Tensor`\n",
"\n",
"No tests found for one_param
. To contribute a test please refer to this guide and this discussion.
range_children
[source][test]range_children
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)) → `Iterator`\\[`int`\\]\n",
"\n",
"trainable_params
[source][test]trainable_params
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)) → `ParamList`\n",
"\n",
"No tests found for trainable_params
. To contribute a test please refer to this guide and this discussion.
bn2float
[source][test]bn2float
(**`module`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)) → [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n",
"\n",
"No tests found for bn2float
. To contribute a test please refer to this guide and this discussion.
set_bn_eval
[source][test]set_bn_eval
(**`m`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module))\n",
"\n",
"split_no_wd_params
[source][test]split_no_wd_params
(**`layer_groups`**:`ModuleList`) → `List`\\[`List`\\[[`Parameter`](https://pytorch.org/docs/stable/nn.html#torch.nn.Parameter)\\]\\]\n",
"\n",
"log_uniform
[source][test]log_uniform
(**`low`**, **`high`**, **`size`**:`Optional`\\[`List`\\[`int`\\]\\]=***`None`***) → `FloatOrTensor`\n",
"\n",
"No tests found for log_uniform
. To contribute a test please refer to this guide and this discussion.
rand_bool
[source][test]rand_bool
(**`p`**:`float`, **`size`**:`Optional`\\[`List`\\[`int`\\]\\]=***`None`***) → `BoolOrTensor`\n",
"\n",
"No tests found for rand_bool
. To contribute a test please refer to this guide and this discussion.
uniform
[source][test]uniform
(**`low`**:`Number`, **`high`**:`Number`=***`None`***, **`size`**:`Optional`\\[`List`\\[`int`\\]\\]=***`None`***) → `FloatOrTensor`\n",
"\n",
"No tests found for uniform
. To contribute a test please refer to this guide and this discussion.
uniform_int
[source][test]uniform_int
(**`low`**:`int`, **`high`**:`int`, **`size`**:`Optional`\\[`List`\\[`int`\\]\\]=***`None`***) → `IntOrTensor`\n",
"\n",
"No tests found for uniform_int
. To contribute a test please refer to this guide and this discussion.
class
ModelOnCPU
[source][test]ModelOnCPU
(**`model`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module))\n",
"\n",
"No tests found for ModelOnCPU
. To contribute a test please refer to this guide and this discussion.
class
NoneReduceOnCPU
[source][test]NoneReduceOnCPU
(**`loss_func`**:`LossFunction`)\n",
"\n",
"class
ParameterModule
[source][test]ParameterModule
(**`p`**:[`Parameter`](https://pytorch.org/docs/stable/nn.html#torch.nn.Parameter)) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n",
"\n",
"No tests found for ParameterModule
. To contribute a test please refer to this guide and this discussion.
data_collate
[source][test]data_collate
(**`batch`**:`ItemsList`) → `Tensor`\n",
"\n",
"No tests found for data_collate
. To contribute a test please refer to this guide and this discussion.
get_model
[source][test]get_model
(**`model`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module))\n",
"\n",
"No tests found for get_model
. To contribute a test please refer to this guide and this discussion.
grab_idx
[source][test]grab_idx
(**`x`**, **`i`**, **`batch_first`**:`bool`=***`True`***)\n",
"\n",
"No tests found for grab_idx
. To contribute a test please refer to this guide and this discussion.
logit
[source][test]logit
(**`x`**:`Tensor`) → `Tensor`\n",
"\n",
"No tests found for logit
. To contribute a test please refer to this guide and this discussion.
logit_
[source][test]logit_
(**`x`**:`Tensor`) → `Tensor`\n",
"\n",
"No tests found for logit_
. To contribute a test please refer to this guide and this discussion.
model_type
[source][test]model_type
(**`dtype`**)\n",
"\n",
"np_address
[source][test]np_address
(**`x`**:`ndarray`) → `int`\n",
"\n",
"split_model
[source][test]split_model
(**`model`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)=***`None`***, **`splits`**:`Collection`\\[`Union`\\[[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), `ModuleList`\\]\\]=***`None`***)\n",
"\n",
"split_model_idx
[source][test]split_model_idx
(**`model`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`idxs`**:`Collection`\\[`int`\\]) → `ModuleList`\n",
"\n",
"No tests found for split_model_idx
. To contribute a test please refer to this guide and this discussion.
trange_of
[source][test]trange_of
(**`x`**)\n",
"\n",
"tensor__array__
[source][test]tensor__array__
(**`dtype`**=***`None`***)\n",
"\n",
"No tests found for tensor__array__
. To contribute a test please refer to this guide and this discussion.
forward
[source][test]forward
(**`x`**)\n",
"\n",
"No tests found for forward
. To contribute a test please refer to this guide and this discussion.
to_float
[source][test]to_float
(**`b`**:`Collection`\\[`Tensor`\\]) → `Collection`\\[`Tensor`\\]\n",
"\n",
"flatten_check
[source][test]flatten_check
(**`out`**:`Tensor`, **`targ`**:`Tensor`) → `Tensor`\n",
"\n",
"No tests found for flatten_check
. To contribute a test please refer to this guide and this discussion.