# Torch Core


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

``` python
from PIL import Image
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L34"
target="_blank" style="float:right; font-size:smaller">source</a>

### setup_cuda

``` python
def setup_cuda(
    benchmark:bool=True
):
```

*Sets the main cuda device and sets `cudnn.benchmark` to `benchmark`*

## Arrays and show

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L44"
target="_blank" style="float:right; font-size:smaller">source</a>

### subplots

``` python
def subplots(
    nrows:int=1, # Number of rows in returned axes grid
    ncols:int=1, # Number of columns in returned axes grid
    figsize:tuple=None, # Width, height in inches of the returned figure
    imsize:int=3, # Size (in inches) of images that will be displayed in the returned figure
    suptitle:str=None, # Title to be set to returned figure
    *, sharex:"bool | Literal['none', 'all', 'row', 'col']"=False,
    sharey:"bool | Literal['none', 'all', 'row', 'col']"=False, squeeze:bool=True,
    width_ratios:Sequence[float] | None=None, height_ratios:Sequence[float] | None=None,
    subplot_kw:dict[str, Any] | None=None, gridspec_kw:dict[str, Any] | None=None, **kwargs
)->(<class 'matplotlib.figure.Figure'>, <class 'matplotlib.axes._axes.Axes'>): # Returns both fig and ax as a tuple
```

*Returns a figure and set of subplots to display images of `imsize`
inches*

This is used in
[`get_grid`](https://docs.fast.ai/vision/vision.data.html#get_grid).
`suptitle`, `sharex`, `sharey`, `squeeze`, `subplot_kw` and
`gridspec_kw` are all passed down to
[plt.subplots](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib-pyplot-subplots).

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L68"
target="_blank" style="float:right; font-size:smaller">source</a>

### show_image

``` python
def show_image(
    im, ax:NoneType=None, figsize:NoneType=None, title:NoneType=None, ctx:NoneType=None, *, cmap:NoneType=None,
    norm:NoneType=None, aspect:NoneType=None, interpolation:NoneType=None, alpha:NoneType=None, vmin:NoneType=None,
    vmax:NoneType=None, colorizer:NoneType=None, origin:NoneType=None, extent:NoneType=None,
    interpolation_stage:NoneType=None, filternorm:bool=True, filterrad:float=4.0, resample:NoneType=None,
    url:NoneType=None, data:NoneType=None, **kwargs
):
```

*Show a PIL or PyTorch image on `ax`.*

[`show_image`](https://docs.fast.ai/core/torch_core.html#show_image) can
show PIL images…

``` python
im = Image.open(TEST_IMAGE_BW)
ax = show_image(im, cmap="Greys")
```

![](00_torch_core_files/figure-commonmark/cell-6-output-1.png)

…and color images with standard `CHW` dim order…

``` python
im2 = np.array(Image.open(TEST_IMAGE))
ax = show_image(im2, figsize=(2,2))
```

![](00_torch_core_files/figure-commonmark/cell-7-output-1.png)

…and color images with `HWC` dim order…

``` python
im3 = torch.as_tensor(im2).permute(2,0,1)
ax = show_image(im3, figsize=(2,2))
```

![](00_torch_core_files/figure-commonmark/cell-8-output-1.png)

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L88"
target="_blank" style="float:right; font-size:smaller">source</a>

### show_titled_image

``` python
def show_titled_image(
    o, *, ax:NoneType=None, figsize:NoneType=None, title:NoneType=None, ctx:NoneType=None, cmap:NoneType=None,
    norm:NoneType=None, aspect:NoneType=None, interpolation:NoneType=None, alpha:NoneType=None, vmin:NoneType=None,
    vmax:NoneType=None, colorizer:NoneType=None, origin:NoneType=None, extent:NoneType=None,
    interpolation_stage:NoneType=None, filternorm:bool=True, filterrad:float=4.0, resample:NoneType=None,
    url:NoneType=None, data:NoneType=None, **kwargs
):
```

*Call
[`show_image`](https://docs.fast.ai/core/torch_core.html#show_image)
destructuring `o` to `(img,title)`*

``` python
show_titled_image((im3,'A puppy'), figsize=(2,2))
```

![](00_torch_core_files/figure-commonmark/cell-10-output-1.png)

Show all images `ims` as subplots with `rows` using `titles`. `suptitle`
provides a way to create a figure title for all images. If you use
`suptitle`, `constrained_layout` is used unless you set
`constrained_layout` to `False`.

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L94"
target="_blank" style="float:right; font-size:smaller">source</a>

### show_images

``` python
def show_images(
    ims, nrows:int=1, # Number of rows in returned axes grid
    ncols:NoneType=None, # Number of columns in returned axes grid
    titles:NoneType=None, *, figsize:tuple=None, # Width, height in inches of the returned figure
    imsize:int=3, # Size (in inches) of images that will be displayed in the returned figure
    suptitle:str=None, # Title to be set to returned figure
    sharex:"bool | Literal['none', 'all', 'row', 'col']"=False,
    sharey:"bool | Literal['none', 'all', 'row', 'col']"=False, squeeze:bool=True,
    width_ratios:Sequence[float] | None=None, height_ratios:Sequence[float] | None=None,
    subplot_kw:dict[str, Any] | None=None, gridspec_kw:dict[str, Any] | None=None
): # Returns both fig and ax as a tuple
```

*Show all images `ims` as subplots with `rows` using `titles`.*

``` python
show_images((im,im3),titles=('number','puppy'),suptitle='Number Puppy',  imsize=3)
```

![](00_torch_core_files/figure-commonmark/cell-12-output-1.png)

[`ArrayImage`](https://docs.fast.ai/core/torch_core.html#arrayimage),
[`ArrayImageBW`](https://docs.fast.ai/core/torch_core.html#arrayimagebw)
and [`ArrayMask`](https://docs.fast.ai/core/torch_core.html#arraymask)
are subclasses of `ndarray` that know how to show themselves.

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L102"
target="_blank" style="float:right; font-size:smaller">source</a>

### ArrayBase

``` python
def ArrayBase(
    *args, **kwargs
):
```

*An `ndarray` that can modify casting behavior*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L108"
target="_blank" style="float:right; font-size:smaller">source</a>

### ArrayImageBase

``` python
def ArrayImageBase(
    *args, **kwargs
):
```

*Base class for arrays representing images*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L115"
target="_blank" style="float:right; font-size:smaller">source</a>

### ArrayImage

``` python
def ArrayImage(
    *args, **kwargs
):
```

*An array representing an image*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L120"
target="_blank" style="float:right; font-size:smaller">source</a>

### ArrayImageBW

``` python
def ArrayImageBW(
    *args, **kwargs
):
```

*An array representing an image*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L125"
target="_blank" style="float:right; font-size:smaller">source</a>

### ArrayMask

``` python
def ArrayMask(
    *args, **kwargs
):
```

*An array representing an image mask*

``` python
im = Image.open(TEST_IMAGE)
```

``` python
im_t = cast(im, ArrayImage)
test_eq(type(im_t), ArrayImage)
```

``` python
ax = im_t.show(figsize=(2,2))
```

![](00_torch_core_files/figure-commonmark/cell-20-output-1.png)

``` python
test_fig_exists(ax)
```

## Basics

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L131"
target="_blank" style="float:right; font-size:smaller">source</a>

### Tensor.\_\_array_eq\_\_

``` python
def __array_eq__(
    b
):
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L147"
target="_blank" style="float:right; font-size:smaller">source</a>

### tensor

``` python
def tensor(
    x, *rest, dtype:NoneType=None, device:NoneType=None, requires_grad:bool=False, pin_memory:bool=False
):
```

*Like `torch.as_tensor`, but handle lists too, and can pass multiple
vector elements directly.*

``` python
test_eq(tensor(torch.tensor([1,2,3])), torch.tensor([1,2,3]))
test_eq(tensor(array([1,2,3])), torch.tensor([1,2,3]))
test_eq(tensor(1,2,3), torch.tensor([1,2,3]))
test_eq_type(tensor(1.0), torch.tensor(1.0))
```

[`set_seed`](https://docs.fast.ai/core/torch_core.html#set_seed) is
useful for reproducibility between runs. It is important to remember
that certain classes such as `Dataloaders` have internal random number
generators that is not effected by this function, so this must be run
before such objects are created in order to guarantee reproducibility.

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L162"
target="_blank" style="float:right; font-size:smaller">source</a>

### set_seed

``` python
def set_seed(
    s, reproducible:bool=False
):
```

*Set random seed for `random`, `torch`, and `numpy` (where available)*

Here is an example of how
[`set_seed`](https://docs.fast.ai/core/torch_core.html#set_seed) can be
used to reset the state of random number generators.

``` python
set_seed(2*33)
a1 = np.random.random()
a2 = torch.rand(())
a3 = random.random()
set_seed(2*33)
b1 = np.random.random()
b2 = torch.rand(())
b3 = random.random()
print('a\'s: {0:3.3f} {1:3.3f} {2:3.3f}'.format(a1,a2,a3))
print('b\'s: {0:3.3f} {1:3.3f} {2:3.3f}'.format(b1,b2,a3))
```

    a's: 0.154 0.498 0.071
    b's: 0.154 0.498 0.071

``` python
test_eq(a1,b1)
test_eq(a2,b2)
test_eq(a3,b3)
```

[`get_random_states`](https://docs.fast.ai/core/torch_core.html#get_random_states)
and
[`set_random_states`](https://docs.fast.ai/core/torch_core.html#set_random_states)
are useful for storing a state so you can go back to it later.

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L176"
target="_blank" style="float:right; font-size:smaller">source</a>

### get_random_states

``` python
def get_random_states():
```

*Gets states for `random`, `torch`, and `numpy` random number
generators*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L186"
target="_blank" style="float:right; font-size:smaller">source</a>

### set_random_states

``` python
def set_random_states(
    random_state, numpy_state, torch_state, torch_cuda_state, torch_deterministic, torch_benchmark
):
```

*Set states for `random`, `torch`, and `numpy` random number generators*

Below notice that the old values and rewinded values are the same
because we were able to return to the previous state.

``` python
old_states = get_random_states()
olds = (random.random(),np.random.random(),torch.rand(()))
news = (random.random(),np.random.random(),torch.rand(()))
set_random_states(**old_states)
rewinds = (random.random(),np.random.random(),torch.rand(()))

print('olds:    {0:3.3f} {1:3.3f} {2:3.3f}'.format(*olds))
print('news:    {0:3.3f} {1:3.3f} {2:3.3f}'.format(*news))
print('rewinds: {0:3.3f} {1:3.3f} {2:3.3f}'.format(*rewinds))
```

    olds:    0.435 0.134 0.023
    news:    0.246 0.363 0.227
    rewinds: 0.435 0.134 0.023

``` python
test_ne(olds,news)
test_eq(olds,rewinds)
```

In [`no_random`](https://docs.fast.ai/core/torch_core.html#no_random) we
combine the ideas of rewinding state with
[`get_random_states`](https://docs.fast.ai/core/torch_core.html#get_random_states)
and
[`set_random_states`](https://docs.fast.ai/core/torch_core.html#set_random_states)
with the ability to
[`set_seed`](https://docs.fast.ai/core/torch_core.html#set_seed) and
create a context manager that can allow us to control randomness in a
portion of our code.

Note: Similar to `torch.random.fork_rng`, but also with `numpy` and
`random`

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L197"
target="_blank" style="float:right; font-size:smaller">source</a>

### no_random

``` python
def no_random(
    seed:int=42, reproducible:bool=True
):
```

*Stores and retrieves state of random number generators. Sets random
seed for `random`, `torch`, and `numpy`.*

Here are some examples on how we can use
[`no_random`](https://docs.fast.ai/core/torch_core.html#no_random) to
control the randomness within a block of code.

``` python
states=get_random_states()
olds = (random.random(),np.random.random(),torch.rand(()))
set_random_states(**states) #rewinding above random calls

with no_random():
    new1 = (random.random(),np.random.random(),torch.rand(()))
with no_random():
    new2 = (random.random(),np.random.random(),torch.rand(()))
with no_random(seed=100):
    seeded1 = (random.random(),np.random.random(),torch.rand(()))
with no_random(seed=100):
    seeded2 = (random.random(),np.random.random(),torch.rand(()))
        
rewinds = (random.random(),np.random.random(),torch.rand(()))

print('olds:    {0:3.3f} {1:3.3f} {2:3.3f}'.format(*olds))
print('new1:    {0:3.3f} {1:3.3f} {2:3.3f}'.format(*new1))
print('new2:    {0:3.3f} {1:3.3f} {2:3.3f}'.format(*new2))
print('seeded1: {0:3.3f} {1:3.3f} {2:3.3f}'.format(*seeded1))
print('seeded2: {0:3.3f} {1:3.3f} {2:3.3f}'.format(*seeded2))
print('rewinds: {0:3.3f} {1:3.3f} {2:3.3f}'.format(*rewinds))
```

    olds:    0.246 0.363 0.227
    new1:    0.639 0.375 0.882
    new2:    0.639 0.375 0.882
    seeded1: 0.146 0.543 0.112
    seeded2: 0.146 0.543 0.112
    rewinds: 0.246 0.363 0.227

Notice that olds, and rewinds are alos both equal to each other. From
this we can see that everything in the `with` blocks did not update the
state outside of the block. Inside of the block, the state is reset for
any particular seed, so for the same seed you should get the same random
number generator results.

Note: It is important to remember that classes like `Dataloader` have
internal random number generators, and
[`no_random`](https://docs.fast.ai/core/torch_core.html#no_random) will
have no effect on those random number generators.

``` python
test_ne(olds,new1)
test_eq(new1,new2)
test_ne(new1,seeded1)
test_eq(seeded1,seeded2)
test_eq(olds,rewinds)
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L207"
target="_blank" style="float:right; font-size:smaller">source</a>

### unsqueeze

``` python
def unsqueeze(
    x, dim:int=-1, n:int=1
):
```

*Same as `torch.unsqueeze` but can add `n` dims*

``` python
t = tensor([1])
t2 = unsqueeze(t, n=2)
test_eq(t2,t[:,None,None])
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L213"
target="_blank" style="float:right; font-size:smaller">source</a>

### unsqueeze\_

``` python
def unsqueeze_(
    x, dim:int=-1, n:int=1
):
```

*Same as `torch.unsqueeze_` but can add `n` dims*

``` python
t = tensor([1])
unsqueeze_(t, n=2)
test_eq(t, tensor([1]).view(1,1,1))
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L223"
target="_blank" style="float:right; font-size:smaller">source</a>

### apply

``` python
def apply(
    func, x, *args, **kwargs
):
```

*Apply `func` recursively to `x`, passing on args*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L231"
target="_blank" style="float:right; font-size:smaller">source</a>

### maybe_gather

``` python
def maybe_gather(
    x, axis:int=0
):
```

*Gather copies of `x` on `axis` (if training is distributed)*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L240"
target="_blank" style="float:right; font-size:smaller">source</a>

### to_detach

``` python
def to_detach(
    b, cpu:bool=True, gather:bool=True
):
```

*Recursively detach lists of tensors in `b`; put them on the CPU if
`cpu=True`.*

`gather` only applies during distributed training and the result tensor
will be the one gathered across processes if `gather=True` (as a result,
the batch size will be multiplied by the number of processes).

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L250"
target="_blank" style="float:right; font-size:smaller">source</a>

### to_half

``` python
def to_half(
    b
):
```

*Recursively map floating point tensors in `b` to FP16.*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L255"
target="_blank" style="float:right; font-size:smaller">source</a>

### to_float

``` python
def to_float(
    b
):
```

*Recursively map floating point tensors in `b` to float.*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L268"
target="_blank" style="float:right; font-size:smaller">source</a>

### default_device

``` python
def default_device(
    use:int=-1
):
```

*Return or set default device; `use_cuda`: -1 - CUDA/mps if available;
True - error if not available; False - CPU*

``` python
if torch.cuda.is_available():
    _td = torch.device(torch.cuda.current_device())
    test_eq(default_device(-1), _td)
    test_eq(default_device(True), _td)
else:
    test_eq(default_device(False), torch.device('cpu'))
default_device(-1);
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L280"
target="_blank" style="float:right; font-size:smaller">source</a>

### to_device

``` python
def to_device(
    b, device:NoneType=None, non_blocking:bool=False
):
```

*Recursively put `b` on `device`.*

``` python
t = to_device((3,(tensor(3),tensor(2))))
t1,(t2,t3) = t
```

``` python
if torch.cuda.is_available():
    test_eq_type(t,(3,(tensor(3).cuda(),tensor(2).cuda())))
    test_eq(t2.type(), "torch.cuda.LongTensor")
    test_eq(t3.type(), "torch.cuda.LongTensor")
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L291"
target="_blank" style="float:right; font-size:smaller">source</a>

### to_cpu

``` python
def to_cpu(
    b
):
```

*Recursively map tensors in `b` to the cpu.*

``` python
t3 = to_cpu(t3)
test_eq(t3.type(), "torch.LongTensor")
test_eq(t3, 2)
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L296"
target="_blank" style="float:right; font-size:smaller">source</a>

### to_np

``` python
def to_np(
    x
):
```

*Convert a tensor to a numpy array.*

``` python
t3 = to_np(t3)
test_eq(type(t3), np.ndarray)
test_eq(t3, 2)
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L301"
target="_blank" style="float:right; font-size:smaller">source</a>

### to_concat

``` python
def to_concat(
    xs, dim:int=0
):
```

*Concat the element in `xs` (recursively if they are tuples/lists of
tensors)*

``` python
test_eq(to_concat([tensor([1,2]), tensor([3,4])]), tensor([1,2,3,4]))
test_eq(to_concat([tensor([[1,2]]), tensor([[3,4]])], dim=1), tensor([[1,2,3,4]]))
test_eq_type(to_concat([(tensor([1,2]), tensor([3,4])), (tensor([3,4]), tensor([5,6]))]), (tensor([1,2,3,4]), tensor([3,4,5,6])))
test_eq_type(to_concat([[tensor([1,2]), tensor([3,4])], [tensor([3,4]), tensor([5,6])]]), [tensor([1,2,3,4]), tensor([3,4,5,6])])
test_eq_type(to_concat([(tensor([1,2]),), (tensor([3,4]),)]), (tensor([1,2,3,4]),))

test_eq(to_concat([tensor([[1,2]]), tensor([[3,4], [5,6]])], dim=1), [tensor([1]),tensor([3, 5]),tensor([4, 6])])
```

``` python
test_eq(type(to_concat([dict(foo=tensor([1,2]), bar=tensor(3,4))])), dict)
```

## Tensor subtypes

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L321"
target="_blank" style="float:right; font-size:smaller">source</a>

### Tensor.set_meta

``` python
def set_meta(
    x, as_copy:bool=False
):
```

*Set all metadata in `__dict__`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L332"
target="_blank" style="float:right; font-size:smaller">source</a>

### Tensor.as_subclass

``` python
def as_subclass(
    typ
):
```

*Cast to `typ` and include `__dict__` and meta*

[`Tensor.set_meta`](https://docs.fast.ai/core/torch_core.html#tensor.set_meta)
and
[`Tensor.as_subclass`](https://docs.fast.ai/core/torch_core.html#tensor.as_subclass)
work together to maintain `__dict__` after casting.

``` python
class _T(Tensor): pass
t = tensor(1.).requires_grad_()
t.img_size = 1
t2 = t.as_subclass(_T)
test_eq(t.img_size, t2.img_size)
test_eq(t2.img_size, 1)
assert(t2.requires_grad_)
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L355"
target="_blank" style="float:right; font-size:smaller">source</a>

### TensorBase

``` python
def TensorBase(
    *args, **kwargs
):
```

*A `Tensor` which support subclass pickling, and maintains metadata when
casting or after methods*

[`TensorBase`](https://docs.fast.ai/core/torch_core.html#tensorbase)
hooks into `__torch_function__` to ensure metadata is not lost. To see
all functions being called, set `debug`.

``` python
a = TensorBase(1)
TensorBase.debug=True
1/(a+1)
```

    <method 'add' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase(1), 1) None
    <function Tensor.__rdiv__> (<class '__main__.TensorBase'>,) (TensorBase(2), 1) {}

    TensorBase(0.5000)

[`TensorBase`](https://docs.fast.ai/core/torch_core.html#tensorbase) and
its subclasses also allow for passing through metadata size as img_size…

``` python
from torch.utils.data._utils.collate import default_collate
```

``` python
a = TensorBase(1,img_size=(128,128))
test_eq(a.img_size,(128,128))
b = cast(a,TensorBase)
test_eq(b.img_size,(128,128))
test_eq(torch.stack([a,b],0).img_size,(128,128))

test_eq(default_collate([a,b]).img_size,(128,128))
```

    <method-wrapper '__get__' of getset_descriptor object> (<class '__main__.TensorBase'>,) (TensorBase(1),) None
    <built-in method stack of type object> (<class '__main__.TensorBase'>,) ([TensorBase(1), TensorBase(1)], 0) None
    <method-wrapper '__get__' of getset_descriptor object> (<class '__main__.TensorBase'>,) (TensorBase(1),) None
    <method-wrapper '__get__' of getset_descriptor object> (<class '__main__.TensorBase'>,) (TensorBase(1),) None
    <built-in method stack of type object> (<class '__main__.TensorBase'>,) ([TensorBase(1), TensorBase(1)], 0) {'out': None}

``` python
class _TImage(TensorBase): pass
class _TImage2(_TImage): pass
t1 = _TImage([1.])
t2 = _TImage2([1.])
t2+t1
```

    <method 'add' of 'torch._C.TensorBase' objects> (<class '__main__._TImage2'>, <class '__main__._TImage'>) (_TImage2([1.]), _TImage([1.])) None

    _TImage2([2.])

``` python
class _T(TensorBase): pass

t = _T(range(5))
test_eq(t[0], 0)
test_eq_type(t+1, _T(range(1,6)))
test_eq(repr(t), '_T([0, 1, 2, 3, 4])')
test_eq_type(t[_T([False,False,True,True,True])], _T([2,3,4]))
test_eq_type(t[_T([2,3,4])], _T([2,3,4]))
test_eq(type(pickle.loads(pickle.dumps(t))), _T)
test_eq_type(t.new_ones(1), _T([1]))
test_eq_type(t.new_tensor([1,2]), _T([1,2]))
```

    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__._T'>,) (_T([0, 1, 2, 3, 4]), 0) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__._T'>,) (_T(0),) None
    <method '__eq__' of 'torch._C.TensorBase' objects> (<class '__main__._T'>,) (_T(0), 0) None
    <method '__bool__' of 'torch._C.TensorBase' objects> (<class '__main__._T'>,) (_T(True),) None
    <method 'add' of 'torch._C.TensorBase' objects> (<class '__main__._T'>,) (_T([0, 1, 2, 3, 4]), 1) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__._T'>,) (_T([1, 2, 3, 4, 5]),) None
    <built-in method equal of type object> (<class '__main__._T'>,) (_T([1, 2, 3, 4, 5]), _T([1, 2, 3, 4, 5])) None
    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__._T'>,) (_T([0, 1, 2, 3, 4]), _T([False, False,  True,  True,  True])) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__._T'>,) (_T([2, 3, 4]),) None
    <built-in method equal of type object> (<class '__main__._T'>,) (_T([2, 3, 4]), _T([2, 3, 4])) None
    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__._T'>,) (_T([0, 1, 2, 3, 4]), _T([2, 3, 4])) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__._T'>,) (_T([2, 3, 4]),) None
    <built-in method equal of type object> (<class '__main__._T'>,) (_T([2, 3, 4]), _T([2, 3, 4])) None
    <function Tensor.__reduce_ex__> (<class '__main__._T'>,) (_T([0, 1, 2, 3, 4]), 4) {}
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__._T'>,) (_T([1]),) None
    <built-in method equal of type object> (<class '__main__._T'>,) (_T([1]), _T([1])) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__._T'>,) (_T([1, 2]),) None
    <built-in method equal of type object> (<class '__main__._T'>,) (_T([1, 2]), _T([1, 2])) None

``` python
t = tensor([1,2,3])
m = TensorBase([False,True,True])
test_eq(t[m], tensor([2,3]))
t = tensor([[1,2,3],[1,2,3]])
m = cast(tensor([[False,True,True],
                 [False,True,True]]), TensorBase)
test_eq(t[m], tensor([2,3,2,3]))
```

    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (tensor([1, 2, 3]), TensorBase([False,  True,  True])) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([2, 3]),) None
    <built-in method equal of type object> (<class '__main__.TensorBase'>,) (TensorBase([2, 3]), tensor([2, 3])) None
    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (tensor([[1, 2, 3],
            [1, 2, 3]]), TensorBase([[False,  True,  True],
                [False,  True,  True]])) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([2, 3, 2, 3]),) None
    <built-in method equal of type object> (<class '__main__.TensorBase'>,) (TensorBase([2, 3, 2, 3]), tensor([2, 3, 2, 3])) None

``` python
t = tensor([[1,2,3],[1,2,3]])
t.img_size = 1
t2 = cast(t, TensorBase)
test_eq(t2.img_size, t.img_size)
x = retain_type(tensor([4,5,6]), t2)
test_eq(x.img_size, t.img_size)
t3 = TensorBase([[1,2,3],[1,2,3]], img_size=1)
test_eq(t3.img_size, t.img_size)
t4 = t2+1
t4.img_size = 2
test_eq(t2.img_size, 1)
test_eq(t4.img_size, 2)
# this will fail with `Tensor` but works with `TensorBase`
test_eq(pickle.loads(pickle.dumps(t2)).img_size, t2.img_size)
```

    <method 'add' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([[1, 2, 3],
                [1, 2, 3]]), 1) None
    <function Tensor.__reduce_ex__> (<class '__main__.TensorBase'>,) (TensorBase([[1, 2, 3],
                [1, 2, 3]]), 4) {}

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L430"
target="_blank" style="float:right; font-size:smaller">source</a>

### TensorImageBase

``` python
def TensorImageBase(
    *args, **kwargs
):
```

*A `Tensor` which support subclass pickling, and maintains metadata when
casting or after methods*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L436"
target="_blank" style="float:right; font-size:smaller">source</a>

### TensorImage

``` python
def TensorImage(
    *args, **kwargs
):
```

*A `Tensor` which support subclass pickling, and maintains metadata when
casting or after methods*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L439"
target="_blank" style="float:right; font-size:smaller">source</a>

### TensorImageBW

``` python
def TensorImageBW(
    *args, **kwargs
):
```

*A `Tensor` which support subclass pickling, and maintains metadata when
casting or after methods*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L442"
target="_blank" style="float:right; font-size:smaller">source</a>

### TensorMask

``` python
def TensorMask(
    *args, **kwargs
):
```

*A `Tensor` which support subclass pickling, and maintains metadata when
casting or after methods*

``` python
im = Image.open(TEST_IMAGE)
im_t = cast(array(im), TensorImage)
test_eq(type(im_t), TensorImage)
```

``` python
im_t2 = cast(tensor(1), TensorMask)
test_eq(type(im_t2), TensorMask)
test_eq(im_t2, tensor(1))
ax = im_t.show(figsize=(2,2))
_ =(im_t == im_t2)
```

    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorMask'>,) (TensorMask(1),) None
    <method '__eq__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorMask'>,) (TensorMask(1), tensor(1)) None
    <method '__bool__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorMask'>,) (TensorMask(True),) None
    <method-wrapper '__get__' of getset_descriptor object> (<class '__main__.TensorImage'>,) (TensorImage([[[192, 191, 199],
                  [195, 194, 202],
                  [199, 198, 204],
                  ...,
                  [140, 142, 154],
                  [146, 148, 160],
                  [152, 154, 166]],

                 [[193, 192, 200],
                  [195, 194, 202],
                  [198, 197, 203],
                  ...,
                  [142, 144, 156],
                  [148, 150, 162],
                  [153, 155, 167]],

                 [[194, 193, 201],
                  [194, 193, 201],
                  [196, 195, 203],
                  ...,
                  [146, 148, 160],
                  [151, 153, 165],
                  [155, 157, 169]],

                 ...,

                 [[160, 160, 162],
                  [159, 159, 161],
                  [161, 161, 163],
                  ...,
                  [201, 202, 207],
                  [207, 206, 212],
                  [209, 210, 215]],

                 [[158, 158, 160],
                  [158, 158, 160],
                  [161, 161, 163],
                  ...,
                  [208, 208, 216],
                  [213, 213, 223],
                  [215, 215, 225]],

                 [[156, 156, 158],
                  [157, 157, 159],
                  [160, 160, 162],
                  ...,
                  [214, 214, 222],
                  [218, 218, 228],
                  [219, 219, 229]]], dtype=torch.uint8),) None
    <method-wrapper '__get__' of getset_descriptor object> (<class '__main__.TensorImage'>,) (TensorImage([[[192, 191, 199],
                  [195, 194, 202],
                  [199, 198, 204],
                  ...,
                  [140, 142, 154],
                  [146, 148, 160],
                  [152, 154, 166]],

                 [[193, 192, 200],
                  [195, 194, 202],
                  [198, 197, 203],
                  ...,
                  [142, 144, 156],
                  [148, 150, 162],
                  [153, 155, 167]],

                 [[194, 193, 201],
                  [194, 193, 201],
                  [196, 195, 203],
                  ...,
                  [146, 148, 160],
                  [151, 153, 165],
                  [155, 157, 169]],

                 ...,

                 [[160, 160, 162],
                  [159, 159, 161],
                  [161, 161, 163],
                  ...,
                  [201, 202, 207],
                  [207, 206, 212],
                  [209, 210, 215]],

                 [[158, 158, 160],
                  [158, 158, 160],
                  [161, 161, 163],
                  ...,
                  [208, 208, 216],
                  [213, 213, 223],
                  [215, 215, 225]],

                 [[156, 156, 158],
                  [157, 157, 159],
                  [160, 160, 162],
                  ...,
                  [214, 214, 222],
                  [218, 218, 228],
                  [219, 219, 229]]], dtype=torch.uint8),) None
    <method 'cpu' of 'torch._C.TensorBase' objects> (<class '__main__.TensorImage'>,) (TensorImage([[[192, 191, 199],
                  [195, 194, 202],
                  [199, 198, 204],
                  ...,
                  [140, 142, 154],
                  [146, 148, 160],
                  [152, 154, 166]],

                 [[193, 192, 200],
                  [195, 194, 202],
                  [198, 197, 203],
                  ...,
                  [142, 144, 156],
                  [148, 150, 162],
                  [153, 155, 167]],

                 [[194, 193, 201],
                  [194, 193, 201],
                  [196, 195, 203],
                  ...,
                  [146, 148, 160],
                  [151, 153, 165],
                  [155, 157, 169]],

                 ...,

                 [[160, 160, 162],
                  [159, 159, 161],
                  [161, 161, 163],
                  ...,
                  [201, 202, 207],
                  [207, 206, 212],
                  [209, 210, 215]],

                 [[158, 158, 160],
                  [158, 158, 160],
                  [161, 161, 163],
                  ...,
                  [208, 208, 216],
                  [213, 213, 223],
                  [215, 215, 225]],

                 [[156, 156, 158],
                  [157, 157, 159],
                  [160, 160, 162],
                  ...,
                  [214, 214, 222],
                  [218, 218, 228],
                  [219, 219, 229]]], dtype=torch.uint8),) None
    <method-wrapper '__get__' of getset_descriptor object> (<class '__main__.TensorImage'>,) (TensorImage([[[192, 191, 199],
                  [195, 194, 202],
                  [199, 198, 204],
                  ...,
                  [140, 142, 154],
                  [146, 148, 160],
                  [152, 154, 166]],

                 [[193, 192, 200],
                  [195, 194, 202],
                  [198, 197, 203],
                  ...,
                  [142, 144, 156],
                  [148, 150, 162],
                  [153, 155, 167]],

                 [[194, 193, 201],
                  [194, 193, 201],
                  [196, 195, 203],
                  ...,
                  [146, 148, 160],
                  [151, 153, 165],
                  [155, 157, 169]],

                 ...,

                 [[160, 160, 162],
                  [159, 159, 161],
                  [161, 161, 163],
                  ...,
                  [201, 202, 207],
                  [207, 206, 212],
                  [209, 210, 215]],

                 [[158, 158, 160],
                  [158, 158, 160],
                  [161, 161, 163],
                  ...,
                  [208, 208, 216],
                  [213, 213, 223],
                  [215, 215, 225]],

                 [[156, 156, 158],
                  [157, 157, 159],
                  [160, 160, 162],
                  ...,
                  [214, 214, 222],
                  [218, 218, 228],
                  [219, 219, 229]]], dtype=torch.uint8),) None
    <method-wrapper '__get__' of getset_descriptor object> (<class '__main__.TensorImage'>,) (TensorImage([[[192, 191, 199],
                  [195, 194, 202],
                  [199, 198, 204],
                  ...,
                  [140, 142, 154],
                  [146, 148, 160],
                  [152, 154, 166]],

                 [[193, 192, 200],
                  [195, 194, 202],
                  [198, 197, 203],
                  ...,
                  [142, 144, 156],
                  [148, 150, 162],
                  [153, 155, 167]],

                 [[194, 193, 201],
                  [194, 193, 201],
                  [196, 195, 203],
                  ...,
                  [146, 148, 160],
                  [151, 153, 165],
                  [155, 157, 169]],

                 ...,

                 [[160, 160, 162],
                  [159, 159, 161],
                  [161, 161, 163],
                  ...,
                  [201, 202, 207],
                  [207, 206, 212],
                  [209, 210, 215]],

                 [[158, 158, 160],
                  [158, 158, 160],
                  [161, 161, 163],
                  ...,
                  [208, 208, 216],
                  [213, 213, 223],
                  [215, 215, 225]],

                 [[156, 156, 158],
                  [157, 157, 159],
                  [160, 160, 162],
                  ...,
                  [214, 214, 222],
                  [218, 218, 228],
                  [219, 219, 229]]], dtype=torch.uint8),) None
    <function Tensor.__array__> (<class '__main__.TensorImage'>,) (TensorImage([[[192, 191, 199],
                  [195, 194, 202],
                  [199, 198, 204],
                  ...,
                  [140, 142, 154],
                  [146, 148, 160],
                  [152, 154, 166]],

                 [[193, 192, 200],
                  [195, 194, 202],
                  [198, 197, 203],
                  ...,
                  [142, 144, 156],
                  [148, 150, 162],
                  [153, 155, 167]],

                 [[194, 193, 201],
                  [194, 193, 201],
                  [196, 195, 203],
                  ...,
                  [146, 148, 160],
                  [151, 153, 165],
                  [155, 157, 169]],

                 ...,

                 [[160, 160, 162],
                  [159, 159, 161],
                  [161, 161, 163],
                  ...,
                  [201, 202, 207],
                  [207, 206, 212],
                  [209, 210, 215]],

                 [[158, 158, 160],
                  [158, 158, 160],
                  [161, 161, 163],
                  ...,
                  [208, 208, 216],
                  [213, 213, 223],
                  [215, 215, 225]],

                 [[156, 156, 158],
                  [157, 157, 159],
                  [160, 160, 162],
                  ...,
                  [214, 214, 222],
                  [218, 218, 228],
                  [219, 219, 229]]], dtype=torch.uint8),) {'dtype': None}
    <method '__eq__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorImage'>,) (TensorImage([[[192, 191, 199],
                  [195, 194, 202],
                  [199, 198, 204],
                  ...,
                  [140, 142, 154],
                  [146, 148, 160],
                  [152, 154, 166]],

                 [[193, 192, 200],
                  [195, 194, 202],
                  [198, 197, 203],
                  ...,
                  [142, 144, 156],
                  [148, 150, 162],
                  [153, 155, 167]],

                 [[194, 193, 201],
                  [194, 193, 201],
                  [196, 195, 203],
                  ...,
                  [146, 148, 160],
                  [151, 153, 165],
                  [155, 157, 169]],

                 ...,

                 [[160, 160, 162],
                  [159, 159, 161],
                  [161, 161, 163],
                  ...,
                  [201, 202, 207],
                  [207, 206, 212],
                  [209, 210, 215]],

                 [[158, 158, 160],
                  [158, 158, 160],
                  [161, 161, 163],
                  ...,
                  [208, 208, 216],
                  [213, 213, 223],
                  [215, 215, 225]],

                 [[156, 156, 158],
                  [157, 157, 159],
                  [160, 160, 162],
                  ...,
                  [214, 214, 222],
                  [218, 218, 228],
                  [219, 219, 229]]], dtype=torch.uint8), TensorMask(1)) None

![](00_torch_core_files/figure-commonmark/cell-72-output-2.png)

``` python
test_fig_exists(ax)
```

Operations between
[`TensorMask`](https://docs.fast.ai/core/torch_core.html#tensormask) and
[`TensorImageBase`](https://docs.fast.ai/core/torch_core.html#tensorimagebase)
objects return the type of the
[`TensorImageBase`](https://docs.fast.ai/core/torch_core.html#tensorimagebase)
object:

``` python
a = TensorMask([1,2])
test_eq_type(TensorImage(1)+a, TensorImage([2,3]))
test_eq_type(1-a, TensorMask([0,-1]))
```

    <method 'add' of 'torch._C.TensorBase' objects> (<class '__main__.TensorImage'>, <class '__main__.TensorMask'>) (TensorImage(1), TensorMask([1, 2])) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorImage'>,) (TensorImage([2, 3]),) None
    <built-in method equal of type object> (<class '__main__.TensorImage'>,) (TensorImage([2, 3]), TensorImage([2, 3])) None
    <function Tensor.__rsub__> (<class '__main__.TensorMask'>,) (TensorMask([1, 2]), 1) {}
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorMask'>,) (TensorMask([ 0, -1]),) None
    <built-in method equal of type object> (<class '__main__.TensorMask'>,) (TensorMask([ 0, -1]), TensorMask([ 0, -1])) None

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L459"
target="_blank" style="float:right; font-size:smaller">source</a>

### TensorFlowField

``` python
def TensorFlowField(
    *args, **kwargs
):
```

*A `Tensor` which support subclass pickling, and maintains metadata when
casting or after methods*

``` python
t1 = TensorImage([1.]).view(1,1,1,1)
t2 = TensorFlowField([1.,1.]).view(1,1,1,2)
test_eq_type(F.grid_sample(t1, t2), TensorImage([[[[0.25]]]]))
```

    <method 'view' of 'torch._C.TensorBase' objects> (<class '__main__.TensorImage'>,) (TensorImage([1.]), 1, 1, 1, 1) None
    <method 'view' of 'torch._C.TensorBase' objects> (<class '__main__.TensorFlowField'>,) (TensorFlowField([1., 1.]), 1, 1, 1, 2) None
    <function grid_sample> (<class '__main__.TensorImage'>, <class '__main__.TensorFlowField'>) (TensorImage([[[[1.]]]]), TensorFlowField([[[[1., 1.]]]])) {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': None}
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorImage'>,) (TensorImage([[[[0.2500]]]]),) None
    <built-in method equal of type object> (<class '__main__.TensorImage'>,) (TensorImage([[[[0.2500]]]]), TensorImage([[[[0.2500]]]])) None

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L463"
target="_blank" style="float:right; font-size:smaller">source</a>

### TensorCategory

``` python
def TensorCategory(
    *args, **kwargs
):
```

*A `Tensor` which support subclass pickling, and maintains metadata when
casting or after methods*

``` python
tc = TensorCategory([1,2,3])
mask_t = TensorMask([0,2,4,5])
im_t = TensorImage([0,2,4,5])
test_eq(mask_t[tc], tensor([2,4,5]))
test_eq(im_t[tc], tensor([2,4,5]))
```

    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorMask'>, <class '__main__.TensorCategory'>) (TensorMask([0, 2, 4, 5]), TensorCategory([1, 2, 3])) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorMask'>,) (TensorMask([2, 4, 5]),) None
    <built-in method equal of type object> (<class '__main__.TensorMask'>,) (TensorMask([2, 4, 5]), tensor([2, 4, 5])) None
    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorImage'>, <class '__main__.TensorCategory'>) (TensorImage([0, 2, 4, 5]), TensorCategory([1, 2, 3])) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorImage'>,) (TensorImage([2, 4, 5]),) None
    <built-in method equal of type object> (<class '__main__.TensorImage'>,) (TensorImage([2, 4, 5]), tensor([2, 4, 5])) None

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L468"
target="_blank" style="float:right; font-size:smaller">source</a>

### TensorMultiCategory

``` python
def TensorMultiCategory(
    *args, **kwargs
):
```

*A `Tensor` which support subclass pickling, and maintains metadata when
casting or after methods*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L471"
target="_blank" style="float:right; font-size:smaller">source</a>

### TitledTensorScalar

``` python
def TitledTensorScalar(
    *args, **kwargs
):
```

*A tensor containing a scalar that has a `show` method*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L485"
target="_blank" style="float:right; font-size:smaller">source</a>

### L.cat

``` python
def cat(
    dim:int=0
):
```

*Same as `torch.cat`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L481"
target="_blank" style="float:right; font-size:smaller">source</a>

### L.stack

``` python
def stack(
    dim:int=0
):
```

*Same as `torch.stack`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L477"
target="_blank" style="float:right; font-size:smaller">source</a>

### L.tensored

``` python
def tensored():
```

*`mapped(tensor)`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L477"
target="_blank" style="float:right; font-size:smaller">source</a>

### L.tensored

``` python
def tensored():
```

*`mapped(tensor)`*

There are shortcuts for `torch.stack` and `torch.cat` if your `L`
contains tensors or something convertible. You can manually convert with
`tensored`.

``` python
t = L(([1,2],[3,4]))
test_eq(t.tensored(), [tensor(1,2),tensor(3,4)])
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L481"
target="_blank" style="float:right; font-size:smaller">source</a>

### L.stack

``` python
def stack(
    dim:int=0
):
```

*Same as `torch.stack`*

``` python
test_eq(t.stack(), tensor([[1,2],[3,4]]))
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L485"
target="_blank" style="float:right; font-size:smaller">source</a>

### L.cat

``` python
def cat(
    dim:int=0
):
```

*Same as `torch.cat`*

``` python
test_eq(t.cat(), tensor([1,2,3,4]))
```

## Chunks

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L490"
target="_blank" style="float:right; font-size:smaller">source</a>

### concat

``` python
def concat(
    *ls
):
```

*Concatenate tensors, arrays, lists, or tuples*

``` python
a,b,c = [1],[1,2],[1,1,2]
test_eq(concat(a,b), c)
test_eq_type(concat(tuple (a),tuple (b)), tuple (c))
test_eq_type(concat(array (a),array (b)), array (c))
test_eq_type(concat(tensor(a),tensor(b)), tensor(c))
test_eq_type(concat(TensorBase(a),TensorBase(b)), TensorBase(c))
test_eq_type(concat([1,1],1), [1,1,1])
test_eq_type(concat(1,1,1), L(1,1,1))
test_eq_type(concat(L(1,2),1), L(1,2,1))
```

    <built-in method cat of type object> (<class '__main__.TensorBase'>,) ((TensorBase([1]), TensorBase([1, 2])),) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([1, 1, 2]),) None
    <built-in method equal of type object> (<class '__main__.TensorBase'>,) (TensorBase([1, 1, 2]), TensorBase([1, 1, 2])) None

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L503"
target="_blank" style="float:right; font-size:smaller">source</a>

### Chunks

``` python
def Chunks(
    chunks, lens:NoneType=None
):
```

*Slice and int indexing into a list of lists*

``` python
docs = L(list(string.ascii_lowercase[a:b]) for a,b in ((0,3),(3,7),(7,8),(8,16),(16,24),(24,26)))

b = Chunks(docs)
test_eq([b[ o] for o in range(0,5)], ['a','b','c','d','e'])
test_eq([b[-o] for o in range(1,6)], ['z','y','x','w','v'])
test_eq(b[6:13], 'g,h,i,j,k,l,m'.split(','))
test_eq(b[20:77], 'u,v,w,x,y,z'.split(','))
test_eq(b[:5], 'a,b,c,d,e'.split(','))
test_eq(b[:2], 'a,b'.split(','))
```

``` python
t = torch.arange(26)
docs = L(t[a:b] for a,b in ((0,3),(3,7),(7,8),(8,16),(16,24),(24,26)))
b = Chunks(docs)
test_eq([b[ o] for o in range(0,5)], range(0,5))
test_eq([b[-o] for o in range(1,6)], [25,24,23,22,21])
test_eq(b[6:13], torch.arange(6,13))
test_eq(b[20:77], torch.arange(20,26))
test_eq(b[:5], torch.arange(5))
test_eq(b[:2], torch.arange(2))
```

``` python
docs = L(TensorBase(t[a:b]) for a,b in ((0,3),(3,7),(7,8),(8,16),(16,24),(24,26)))
b = Chunks(docs)
test_eq_type(b[:2], TensorBase(range(2)))
test_eq_type(b[:5], TensorBase(range(5)))
test_eq_type(b[9:13], TensorBase(range(9,13)))
```

    <function Tensor.__len__> (<class '__main__.TensorBase'>,) (TensorBase([0, 1, 2]),) {}
    <function Tensor.__len__> (<class '__main__.TensorBase'>,) (TensorBase([3, 4, 5, 6]),) {}
    <function Tensor.__len__> (<class '__main__.TensorBase'>,) (TensorBase([7]),) {}
    <function Tensor.__len__> (<class '__main__.TensorBase'>,) (TensorBase([ 8,  9, 10, 11, 12, 13, 14, 15]),) {}
    <function Tensor.__len__> (<class '__main__.TensorBase'>,) (TensorBase([16, 17, 18, 19, 20, 21, 22, 23]),) {}
    <function Tensor.__len__> (<class '__main__.TensorBase'>,) (TensorBase([24, 25]),) {}
    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([0, 1, 2]), slice(0, 2, None)) None
    <built-in method cat of type object> (<class '__main__.TensorBase'>,) ((TensorBase([0, 1]),),) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([0, 1]),) None
    <built-in method equal of type object> (<class '__main__.TensorBase'>,) (TensorBase([0, 1]), TensorBase([0, 1])) None
    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([0, 1, 2]), slice(0, 9223372036854775807, None)) None
    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([3, 4, 5, 6]), slice(None, 2, None)) None
    <built-in method cat of type object> (<class '__main__.TensorBase'>,) ((TensorBase([0, 1, 2]), TensorBase([3, 4])),) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([0, 1, 2, 3, 4]),) None
    <built-in method equal of type object> (<class '__main__.TensorBase'>,) (TensorBase([0, 1, 2, 3, 4]), TensorBase([0, 1, 2, 3, 4])) None
    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([ 8,  9, 10, 11, 12, 13, 14, 15]), slice(1, 5, None)) None
    <built-in method cat of type object> (<class '__main__.TensorBase'>,) ((TensorBase([ 9, 10, 11, 12]),),) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([ 9, 10, 11, 12]),) None
    <built-in method equal of type object> (<class '__main__.TensorBase'>,) (TensorBase([ 9, 10, 11, 12]), TensorBase([ 9, 10, 11, 12])) None

## Simple types

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L531"
target="_blank" style="float:right; font-size:smaller">source</a>

### show_title

``` python
def show_title(
    o, ax:NoneType=None, ctx:NoneType=None, label:NoneType=None, color:str='black', **kwargs
):
```

*Set title of `ax` to `o`, or print `o` if `ax` is `None`*

``` python
test_stdout(lambda: show_title("title"), "title")
# ensure that col names are unique when showing to a pandas series
assert show_title("title", ctx=pd.Series(dict(a=1)), label='a').equals(pd.Series(dict(a=1,a_='title')))
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L545"
target="_blank" style="float:right; font-size:smaller">source</a>

### ShowTitle

``` python
def ShowTitle(
    *args, **kwargs
):
```

*Base class that adds a simple `show`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L552"
target="_blank" style="float:right; font-size:smaller">source</a>

### TitledInt

``` python
def TitledInt(
    *args, **kwargs
):
```

*An `int` with `show`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L564"
target="_blank" style="float:right; font-size:smaller">source</a>

### TitledStr

``` python
def TitledStr(
    *args, **kwargs
):
```

*An `str` with `show`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L558"
target="_blank" style="float:right; font-size:smaller">source</a>

### TitledFloat

``` python
def TitledFloat(
    *args, **kwargs
):
```

*A `float` with `show`*

``` python
test_stdout(lambda: TitledStr('s').show(), 's')
test_stdout(lambda: TitledInt(1).show(), '1')
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L570"
target="_blank" style="float:right; font-size:smaller">source</a>

### TitledTuple

``` python
def TitledTuple(
    *args, **kwargs
):
```

*A `fastuple` with `show`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L581"
target="_blank" style="float:right; font-size:smaller">source</a>

### TitledStr.truncate

``` python
def truncate(
    n
):
```

*Truncate self to `n`*

## Other functions

------------------------------------------------------------------------

### DataFrame.\_\_init\_\_

``` python
def __init__(
    data:NoneType=None, index:NoneType=None, columns:NoneType=None, dtype:NoneType=None, copy:NoneType=None
):
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L596"
target="_blank" style="float:right; font-size:smaller">source</a>

### get_empty_df

``` python
def get_empty_df(
    n
):
```

*Return `n` empty rows of a dataframe*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L602"
target="_blank" style="float:right; font-size:smaller">source</a>

### display_df

``` python
def display_df(
    df
):
```

*Display `df` in a notebook or defaults to print*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L609"
target="_blank" style="float:right; font-size:smaller">source</a>

### get_first

``` python
def get_first(
    c
):
```

*Get the first element of c, even if c is a dataframe*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L614"
target="_blank" style="float:right; font-size:smaller">source</a>

### one_param

``` python
def one_param(
    m
):
```

*First parameter in `m`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L619"
target="_blank" style="float:right; font-size:smaller">source</a>

### item_find

``` python
def item_find(
    x, idx:int=0
):
```

*Recursively takes the `idx`-th element of `x`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L628"
target="_blank" style="float:right; font-size:smaller">source</a>

### find_device

``` python
def find_device(
    b
):
```

*Recursively search the device of `b`.*

``` python
t2 = to_device(tensor(0))
dev = default_device()
test_eq(find_device(t2), dev)
test_eq(find_device([t2,t2]), dev)
test_eq(find_device({'a':t2,'b':t2}), dev)
test_eq(find_device({'a':[[t2],[t2]],'b':t2}), dev)
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L633"
target="_blank" style="float:right; font-size:smaller">source</a>

### find_bs

``` python
def find_bs(
    b
):
```

*Recursively search the batch size of `b`.*

``` python
x = torch.randn(4,5)
x1 = [1,2,3]
test_eq(find_bs(x1), 3)
test_eq(find_bs(x), 4)
test_eq(find_bs((x,x)), 4)
test_eq(find_bs([x, x]), 4)
test_eq(find_bs({'a':x,'b':x}), 4)
test_eq(find_bs({'a':[[x],[x]],'b':x}), 4)
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L640"
target="_blank" style="float:right; font-size:smaller">source</a>

### np_func

``` python
def np_func(
    f
):
```

*Convert a function taking and returning numpy arrays to one taking and
returning tensors*

This decorator is particularly useful for using numpy functions as
fastai metrics, for instance:

``` python
from sklearn.metrics import f1_score
```

``` python
@np_func
def f1(inp,targ): return f1_score(targ, inp)

a1,a2 = array([0,1,1]),array([1,0,1])
t = f1(tensor(a1),tensor(a2))
test_eq(f1_score(a1,a2), t)
assert isinstance(t,Tensor)
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L649"
target="_blank" style="float:right; font-size:smaller">source</a>

### Module

``` python
def Module():
```

*Same as `nn.Module`, but no need for subclasses to call
`super().__init__`*

``` python
class _T(Module):
    def __init__(self): self.f = nn.Linear(1,1)
    def forward(self,x): return self.f(x)

t = _T()
t(tensor([1.]))
```

    tensor([0.9937], grad_fn=<ViewBackward0>)

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L658"
target="_blank" style="float:right; font-size:smaller">source</a>

### get_model

``` python
def get_model(
    model
):
```

*Return the model maybe wrapped inside `model`.*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L663"
target="_blank" style="float:right; font-size:smaller">source</a>

### one_hot

``` python
def one_hot(
    x, c
):
```

*One-hot encode `x` with `c` classes.*

``` python
test_eq(one_hot([1,4], 5), tensor(0,1,0,0,1).byte())
test_eq(one_hot(torch.tensor([]), 5), tensor(0,0,0,0,0).byte())
test_eq(one_hot(2, 5), tensor(0,0,1,0,0).byte())
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L671"
target="_blank" style="float:right; font-size:smaller">source</a>

### one_hot_decode

``` python
def one_hot_decode(
    x, vocab:NoneType=None
):
```

``` python
test_eq(one_hot_decode(tensor(0,1,0,0,1)), [1,4])
test_eq(one_hot_decode(tensor(0,0,0,0,0)), [   ])
test_eq(one_hot_decode(tensor(0,0,1,0,0)), [2  ])
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L675"
target="_blank" style="float:right; font-size:smaller">source</a>

### params

``` python
def params(
    m
):
```

*Return all parameters of `m`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L680"
target="_blank" style="float:right; font-size:smaller">source</a>

### trainable_params

``` python
def trainable_params(
    m
):
```

*Return all trainable parameters of `m`*

``` python
m = nn.Linear(4,5)
test_eq(trainable_params(m), [m.weight, m.bias])
m.weight.requires_grad_(False)
test_eq(trainable_params(m), [m.bias])
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L688"
target="_blank" style="float:right; font-size:smaller">source</a>

### norm_bias_params

``` python
def norm_bias_params(
    m, with_bias:bool=True
):
```

*Return all bias and BatchNorm parameters*

``` python
for norm_func in [nn.BatchNorm1d, partial(nn.InstanceNorm1d, affine=True)]:
    model = nn.Sequential(nn.Linear(10,20), norm_func(20), nn.Conv1d(3,4, 3))
    test_eq(norm_bias_params(model), [model[0].bias, model[1].weight, model[1].bias, model[2].bias])
    model = nn.ModuleList([nn.Linear(10,20, bias=False), nn.Sequential(norm_func(20), nn.Conv1d(3,4,3))])
    test_eq(norm_bias_params(model), [model[1][0].weight, model[1][0].bias, model[1][1].bias])
    model = nn.ModuleList([nn.Linear(10,20), nn.Sequential(norm_func(20), nn.Conv1d(3,4,3))])
    test_eq(norm_bias_params(model, with_bias=False), [model[1][0].weight, model[1][0].bias])
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L696"
target="_blank" style="float:right; font-size:smaller">source</a>

### batch_to_samples

``` python
def batch_to_samples(
    b, max_n:int=10
):
```

*‘Transposes’ a batch to (at most `max_n`) samples*

``` python
t = tensor([1,2,3])
test_eq(batch_to_samples([t,t+1], max_n=2), ([1,2],[2,3]))
test_eq(batch_to_samples(tensor([1,2,3]), 10), [1, 2, 3])
test_eq(batch_to_samples([tensor([1,2,3]), tensor([4,5,6])], 10), [(1, 4), (2, 5), (3, 6)])
test_eq(batch_to_samples([tensor([1,2,3]), tensor([4,5,6])], 2), [(1, 4), (2, 5)])
test_eq(batch_to_samples([tensor([1,2,3]), [tensor([4,5,6]),tensor([7,8,9])]], 10), 
        [(1, (4, 7)), (2, (5, 8)), (3, (6, 9))])
test_eq(batch_to_samples([tensor([1,2,3]), [tensor([4,5,6]),tensor([7,8,9])]], 2), [(1, (4, 7)), (2, (5, 8))])

t = fastuple(tensor([1,2,3]),TensorBase([2,3,4]))
test_eq_type(batch_to_samples(t)[0][1], TensorBase(2))
test_eq(batch_to_samples(t).map(type), [fastuple]*3)
```

    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([2, 3, 4]), slice(None, 10, None)) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([2, 3, 4]),) None
    <method 'unbind' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([2, 3, 4]), 0) None
    <function Tensor.__len__> (<class '__main__.TensorBase'>,) (TensorBase([2, 3, 4]),) {}
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase(2),) None
    <method '__eq__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase(2), TensorBase(2)) None
    <method '__bool__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase(True),) None
    <slot wrapper '__getitem__' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([2, 3, 4]), slice(None, 10, None)) None
    <method 'dim' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([2, 3, 4]),) None
    <method 'unbind' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([2, 3, 4]), 0) None
    <function Tensor.__len__> (<class '__main__.TensorBase'>,) (TensorBase([2, 3, 4]),) {}

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L705"
target="_blank" style="float:right; font-size:smaller">source</a>

### Tensor.interp_1d

``` python
def interp_1d(
    x:torch.Tensor, xp, fp
):
```

*Same as `np.interp`*

``` python
brks = tensor(0,1,2,4,8,64).float()
ys = tensor(range_of(brks)).float()
ys /= ys[-1].item()
pts = tensor(0.2,0.5,0.8,3,5,63)

preds = pts.interp_1d(brks, ys)
test_close(preds.numpy(), np.interp(pts.numpy(), brks.numpy(), ys.numpy()))

plt.scatter(brks,ys)
plt.scatter(pts,preds)
plt.legend(['breaks','preds']);
```

![](00_torch_core_files/figure-commonmark/cell-133-output-1.png)

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L715"
target="_blank" style="float:right; font-size:smaller">source</a>

### Tensor.pca

``` python
def pca(
    x:torch.Tensor, k:int=2
):
```

*Compute PCA of `x` with `k` dimensions.*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L722"
target="_blank" style="float:right; font-size:smaller">source</a>

### logit

``` python
def logit(
    x
):
```

*Logit of `x`, clamped to avoid inf.*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L728"
target="_blank" style="float:right; font-size:smaller">source</a>

### num_distrib

``` python
def num_distrib():
```

*Return the number of processes in distributed training (if
applicable).*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L733"
target="_blank" style="float:right; font-size:smaller">source</a>

### rank_distrib

``` python
def rank_distrib():
```

*Return the distributed rank of this process (if applicable).*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L738"
target="_blank" style="float:right; font-size:smaller">source</a>

### distrib_barrier

``` python
def distrib_barrier():
```

*Place a synchronization barrier in distributed training*

After calling this, ALL sub-processes in the pytorch process group must
arrive here before proceeding.

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L752"
target="_blank" style="float:right; font-size:smaller">source</a>

### Path.save_array

``` python
def save_array(
    p:pathlib.Path, o, complib:str='lz4', lvl:int=3
):
```

*Save numpy array to a compressed `pytables` file, using compression
level `lvl`*

Compression lib can be any of: blosclz, lz4, lz4hc, snappy, zlib or
zstd.

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L759"
target="_blank" style="float:right; font-size:smaller">source</a>

### Path.load_array

``` python
def load_array(
    p:pathlib.Path
):
```

*Save numpy array to a `pytables` file*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L764"
target="_blank" style="float:right; font-size:smaller">source</a>

### base_doc

``` python
def base_doc(
    elt
):
```

*Print a base documentation of `elt`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L771"
target="_blank" style="float:right; font-size:smaller">source</a>

### doc

``` python
def doc(
    elt
):
```

*Try to use doc form nbdev and fall back to
[`base_doc`](https://docs.fast.ai/core/torch_core.html#base_doc)*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L779"
target="_blank" style="float:right; font-size:smaller">source</a>

### nested_reorder

``` python
def nested_reorder(
    t, idxs
):
```

*Reorder all tensors in `t` using `idxs`*

``` python
x = tensor([0,1,2,3,4,5])
idxs = tensor([2,5,1,0,3,4])
test_eq_type(nested_reorder(([x], x), idxs), ([idxs], idxs))

y = L(0,1,2,3,4,5)
z = L(i.item() for i in idxs)
test_eq_type(nested_reorder((y, x), idxs), (z,idxs))
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L787"
target="_blank" style="float:right; font-size:smaller">source</a>

### flatten_check

``` python
def flatten_check(
    inp, targ
):
```

*Check that `inp` and `targ` have the same number of elements and
flatten them.*

``` python
x1,x2 = torch.randn(5,4),torch.randn(20)
x1,x2 = flatten_check(x1,x2)
test_eq(x1.shape, [20])
test_eq(x2.shape, [20])
x1,x2 = torch.randn(5,4),torch.randn(21)
with expect_fail(): flatten_check(x1,x2)
```

    <method 'view' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([[ 1.0382,  0.2694,  0.1557,  0.8046],
                [ 0.2153, -0.8074,  1.3402, -0.5470],
                [-0.9837, -1.5019,  1.0357,  0.1674],
                [ 0.4258,  0.3218,  0.4773, -0.4644],
                [-0.8298, -0.0215, -0.5018,  2.3215]]), -1) None
    <method 'view' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([ 2.8900, -1.3661,  0.1912,  0.5172, -1.3732, -0.5845, -0.7272,
                -0.0853, -0.0387,  0.8470,  1.3114, -0.0933, -1.6411, -0.3346,
                 0.9161,  1.1654,  0.2641, -1.2561,  0.9964,  1.1314]), -1) None
    <function Tensor.__len__> (<class '__main__.TensorBase'>,) (TensorBase([ 1.0382,  0.2694,  0.1557,  0.8046,  0.2153, -0.8074,  1.3402,
                -0.5470, -0.9837, -1.5019,  1.0357,  0.1674,  0.4258,  0.3218,
                 0.4773, -0.4644, -0.8298, -0.0215, -0.5018,  2.3215]),) {}
    <function Tensor.__len__> (<class '__main__.TensorBase'>,) (TensorBase([ 2.8900, -1.3661,  0.1912,  0.5172, -1.3732, -0.5845, -0.7272,
                -0.0853, -0.0387,  0.8470,  1.3114, -0.0933, -1.6411, -0.3346,
                 0.9161,  1.1654,  0.2641, -1.2561,  0.9964,  1.1314]),) {}
    <method-wrapper '__get__' of getset_descriptor object> (<class '__main__.TensorBase'>,) (TensorBase([ 1.0382,  0.2694,  0.1557,  0.8046,  0.2153, -0.8074,  1.3402,
                -0.5470, -0.9837, -1.5019,  1.0357,  0.1674,  0.4258,  0.3218,
                 0.4773, -0.4644, -0.8298, -0.0215, -0.5018,  2.3215]),) None
    <method-wrapper '__get__' of getset_descriptor object> (<class '__main__.TensorBase'>,) (TensorBase([ 2.8900, -1.3661,  0.1912,  0.5172, -1.3732, -0.5845, -0.7272,
                -0.0853, -0.0387,  0.8470,  1.3114, -0.0933, -1.6411, -0.3346,
                 0.9161,  1.1654,  0.2641, -1.2561,  0.9964,  1.1314]),) None
    <method 'view' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([[-1.7181, -0.2147, -1.2145, -1.6510],
                [ 0.3447, -1.0924,  0.1994, -0.0726],
                [-0.5407,  0.0623, -0.0966, -0.0448],
                [ 0.7951, -0.2282, -0.8035,  1.5779],
                [-0.3452,  0.3078,  1.3254, -0.4378]]), -1) None
    <method 'view' of 'torch._C.TensorBase' objects> (<class '__main__.TensorBase'>,) (TensorBase([-0.1303,  0.4928, -0.3458, -0.5185,  1.4189,  1.1956, -1.2492,
                 1.0312, -0.4071,  0.4928,  0.3276, -0.3133, -0.3452, -0.2146,
                -0.9111,  0.2789,  0.4154,  1.4559, -1.5123, -1.3805, -0.3555]), -1) None
    <function Tensor.__len__> (<class '__main__.TensorBase'>,) (TensorBase([-1.7181, -0.2147, -1.2145, -1.6510,  0.3447, -1.0924,  0.1994,
                -0.0726, -0.5407,  0.0623, -0.0966, -0.0448,  0.7951, -0.2282,
                -0.8035,  1.5779, -0.3452,  0.3078,  1.3254, -0.4378]),) {}
    <function Tensor.__len__> (<class '__main__.TensorBase'>,) (TensorBase([-0.1303,  0.4928, -0.3458, -0.5185,  1.4189,  1.1956, -1.2492,
                 1.0312, -0.4071,  0.4928,  0.3276, -0.3133, -0.3452, -0.2146,
                -0.9111,  0.2789,  0.4154,  1.4559, -1.5123, -1.3805, -0.3555]),) {}

## Image helpers

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L794"
target="_blank" style="float:right; font-size:smaller">source</a>

### make_cross_image

``` python
def make_cross_image(
    bw:bool=True
):
```

*Create a tensor containing a cross image, either `bw` (True) or color*

``` python
plt.imshow(make_cross_image(), cmap="Greys");
```

![](00_torch_core_files/figure-commonmark/cell-148-output-1.png)

``` python
plt.imshow(make_cross_image(False).permute(1,2,0));
```

![](00_torch_core_files/figure-commonmark/cell-149-output-1.png)

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L807"
target="_blank" style="float:right; font-size:smaller">source</a>

### show_image_batch

``` python
def show_image_batch(
    b, show:function=show_titled_image, items:int=9, cols:int=3, figsize:NoneType=None, **kwargs
):
```

*Display batch `b` in a grid of size `items` with `cols` width*

``` python
show_image_batch(([Image.open(TEST_IMAGE_BW),Image.open(TEST_IMAGE)],['bw','color']), items=2)
```

![](00_torch_core_files/figure-commonmark/cell-151-output-1.png)

## Model init

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L816"
target="_blank" style="float:right; font-size:smaller">source</a>

### requires_grad

``` python
def requires_grad(
    m
):
```

*Check if the first parameter of `m` requires grad or not*

``` python
tst = nn.Linear(4,5)
assert requires_grad(tst)
for p in tst.parameters(): p.requires_grad_(False)
assert not requires_grad(tst)
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/layers.py#L215"
target="_blank" style="float:right; font-size:smaller">source</a>

### init_default

``` python
def init_default(
    m, func:function=kaiming_normal_
):
```

*Initialize `m` weights with `func` and set `bias` to 0.*

``` python
tst = nn.Linear(4,5)
tst.weight.data.uniform_(-1,1)
tst.bias.data.uniform_(-1,1)
tst = init_default(tst, func = lambda x: x.data.fill_(1.))
test_eq(tst.weight, torch.ones(5,4))
test_eq(tst.bias, torch.zeros(5))
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L830"
target="_blank" style="float:right; font-size:smaller">source</a>

### cond_init

``` python
def cond_init(
    m, func
):
```

*Apply
[`init_default`](https://docs.fast.ai/core/layers.html#init_default) to
`m` unless it’s a batchnorm module*

``` python
tst = nn.Linear(4,5)
tst.weight.data.uniform_(-1,1)
tst.bias.data.uniform_(-1,1)
cond_init(tst, func = lambda x: x.data.fill_(1.))
test_eq(tst.weight, torch.ones(5,4))
test_eq(tst.bias, torch.zeros(5))

tst = nn.BatchNorm2d(5)
init = [tst.weight.clone(), tst.bias.clone()]
cond_init(tst, func = lambda x: x.data.fill_(1.))
test_eq(tst.weight, init[0])
test_eq(tst.bias, init[1])
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L835"
target="_blank" style="float:right; font-size:smaller">source</a>

### apply_leaf

``` python
def apply_leaf(
    m, f
):
```

*Apply `f` to children of `m`.*

``` python
tst = nn.Sequential(nn.Linear(4,5), nn.Sequential(nn.Linear(4,5), nn.Linear(4,5)))
apply_leaf(tst, partial(init_default, func=lambda x: x.data.fill_(1.)))
for l in [tst[0], *tst[1]]: test_eq(l.weight, torch.ones(5,4))
for l in [tst[0], *tst[1]]: test_eq(l.bias,   torch.zeros(5))
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L842"
target="_blank" style="float:right; font-size:smaller">source</a>

### apply_init

``` python
def apply_init(
    m, func:function=kaiming_normal_
):
```

*Initialize all non-batchnorm layers of `m` with `func`.*

``` python
tst = nn.Sequential(nn.Linear(4,5), nn.Sequential(nn.Linear(4,5), nn.BatchNorm1d(5)))
init = [tst[1][1].weight.clone(), tst[1][1].bias.clone()]
apply_init(tst, func=lambda x: x.data.fill_(1.))
for l in [tst[0], tst[1][0]]: test_eq(l.weight, torch.ones(5,4))
for l in [tst[0], tst[1][0]]: test_eq(l.bias,   torch.zeros(5))
test_eq(tst[1][1].weight, init[0])
test_eq(tst[1][1].bias,   init[1])
```

## autograd jit functions

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L847"
target="_blank" style="float:right; font-size:smaller">source</a>

### script_use_ctx

``` python
def script_use_ctx(
    f
):
```

*Decorator: create jit script and pass everything in
`ctx.saved_variables to`f`, after`*args\`\*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L854"
target="_blank" style="float:right; font-size:smaller">source</a>

### script_save_ctx

``` python
def script_save_ctx(
    static, *argidx
):
```

*Decorator: create jit script and save args with indices `argidx` using
`ctx.save_for_backward`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L869"
target="_blank" style="float:right; font-size:smaller">source</a>

### script_fwd

``` python
def script_fwd(
    *argidx
):
```

*Decorator: create static jit script and save args with indices `argidx`
using `ctx.save_for_backward`*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L874"
target="_blank" style="float:right; font-size:smaller">source</a>

### script_bwd

``` python
def script_bwd(
    f
):
```

*Decorator: create static jit script and pass everything in
`ctx.saved_variables to`f`, after`*args\`\*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L879"
target="_blank" style="float:right; font-size:smaller">source</a>

### grad_module

``` python
def grad_module(
    cls
):
```

*Decorator: convert `cls` into an autograd function*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L886"
target="_blank" style="float:right; font-size:smaller">source</a>

### ismin_torch

``` python
def ismin_torch(
    min_version
):
```

*Check if `torch.__version__` \>= `min_version` using packaging.version*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/torch_core.py#L891"
target="_blank" style="float:right; font-size:smaller">source</a>

### notmax_torch

``` python
def notmax_torch(
    max_version
):
```

*Check if `torch.__version__` \< `max_version` using packaging.version*
