bounds.padding
This module reimplements torch.nn.functional.pad and torch.roll
with a larger set of boundary conditions.
| FUNCTION | DESCRIPTION |
|---|---|
pad |
Pad a tensor |
roll |
Roll a tensor |
ensure_shape |
Pad/crop a tensor so that it has a given shape |
pad
Pad a tensor.
This function is a bit more generic than torch's native pad
(torch.nn.functional.pad), but probably a bit slower:
- works with any input type
- works with arbitrarily large padding size
- crops the tensor for negative padding values
- implements additional padding modes
When used with defaults parameters (side=None), it behaves
exactly like torch.nn.functional.pad
Boundary modes
Like in PyTorch's pad, boundary modes include:
'circular'(or'dft')'mirror'(or'dct1')'reflect'(or'dct2')'replicate'(or'nearest')'constant'(or'zero')
as well as the following new modes:
'antimirror'(or'dst1')'antireflect'(or'dst2')
Side modes
Side modes are 'pre' (or 'left'), 'post' (or 'right'),
'both' or None.
- If side is not
None,inp.dim()values (or less) should be provided. - If side is
None, twice as many values should be provided, indicating different padding sizes for the'pre'and'post'sides. - If the number of padding values is less than the dimension of the input tensor, zeros are prepended.
| PARAMETER | DESCRIPTION |
|---|---|
inp |
Input tensor
TYPE:
|
padsize |
Amount of padding in each dimension.
TYPE:
|
mode |
Padding mode
TYPE:
|
value |
Value to pad with in mode
TYPE:
|
side |
Use padsize to pad on left side (
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
out
|
Padded tensor.
TYPE:
|
Source code in bounds/padding.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
ensure_shape
Pad/crop a tensor so that it has a given shape
| PARAMETER | DESCRIPTION |
|---|---|
inp |
Input tensor
TYPE:
|
shape |
Output shape
TYPE:
|
mode |
Boundary mode
TYPE:
|
value |
Value for mode
TYPE:
|
side |
Side to crop/pad
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
out
|
Padded tensor with shape
TYPE:
|
Source code in bounds/padding.py
roll
Like torch.roll, but with any boundary condition
Warning
When dims is None, we do not flatten but shift all dimensions.
This differs from the behavior of torch.roll .
| PARAMETER | DESCRIPTION |
|---|---|
inp |
Input
TYPE:
|
shifts |
Amount by which to roll. Positive shifts to the right, negative to the left.
TYPE:
|
dims |
Dimensions to roll. By default, shifts apply to all dimensions if a scalar, or to the last N if a sequence.
TYPE:
|
bound |
Boundary condition
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
out
|
Rolled tensor
TYPE:
|