Functionals
einconv.functionals.convNd
convNd(x: Tensor, weight: Union[Tensor, Parameter], bias: Union[Tensor, Parameter, None] = None, stride: Union[int, Tuple[int, ...]] = 1, padding: Union[int, str, Tuple[int, ...]] = 0, dilation: Union[int, Tuple[int, ...]] = 1, groups: int = 1, simplify: bool = True) -> Tensor
Generalization of torch.nn.functional.conv{1,2,3}d
to N
d.
N
is determined from the input tensor: It's first axis is the batch dimension,
the second axis the channel dimension, and the remaining number of dimensions is
interpreted as spatial dimension (with number of spatial dimensions N
)
Parameters:
-
x
(Tensor
) –Convolution input. Has shape
[batch_size, in_channels, *input_sizes]
wherelen(input_sizes) == N
. -
weight
(Union[Tensor, Parameter]
) –Kernel of the convolution. Has shape
[out_channels, in_channels / groups, *kernel_size]
wherekernel_size
is anN
-tuple of kernel dimensions. -
bias
(Union[Tensor, Parameter, None]
, default:None
) –Optional bias vector of the convolution. Has shape
[out_channels]
. Default:None
. -
stride
(Union[int, Tuple[int, ...]]
, default:1
) –Stride of the convolution. Can be a single integer (shared along all spatial dimensions), or an
N
-tuple of integers. Default:1
. -
padding
(Union[int, str, Tuple[int, ...]]
, default:0
) –Padding of the convolution. Can be a single integer (shared along all spatial dimensions), an
N
-tuple of integers, or a string. Default:0
. Allowed strings are'same'
and'valid'
. -
dilation
(Union[int, Tuple[int, ...]]
, default:1
) –Dilation of the convolution. Can be a single integer (shared along all spatial dimensions), or an
N
-tuple of integers. Default:1
. -
groups
(int
, default:1
) –In how many groups to split the input channels. Default:
1
. -
simplify
(bool
, default:True
) –Whether to use a simplified einsum expression. Default:
True
.
Returns:
-
Tensor
–Result of the convolution. Has shape
[batch_size, out_channels, *output_sizes]
wherelen(output_sizes) == N
. Ineinops
notation, the index structure isn (g c_out) o1 o2 ...
.
Source code in einconv/functionals/conv.py
einconv.functionals.unfoldNd
unfoldNd(x: Tensor, kernel_size: Union[int, Tuple[int, ...]], dilation: Union[int, Tuple[int, ...]] = 1, padding: Union[int, Tuple[int, ...], str] = 0, stride: Union[int, Tuple[int, ...]] = 1, simplify: bool = True) -> Tensor
Torch functional for N-dimensional input unfolding that uses einsum.
Extracts sliding local blocks from a batched input tensor (im2col
).
Accepts batched tensors with N
spatial dimensions. Acts like
torch.nn.functional.unfold
for a 4d input (batched images), but works for
arbitrary N
. See https://pytorch.org/docs/stable/nn.functional.html#unfold.
Parameters:
-
x
(Tensor
) –Convolution input. Has shape
[batch_size, in_channels, *input_sizes]
wherelen(input_sizes) == N
. -
kernel_size
(Union[int, Tuple[int, ...]]
) –Kernel dimensions. Can be a single integer (shared along all spatial dimensions), or an
N
-tuple of integers. -
dilation
(Union[int, Tuple[int, ...]]
, default:1
) –Dilation of the convolution. Can be a single integer (shared along all spatial dimensions), or an
N
-tuple of integers. Default:1
. -
padding
(Union[int, Tuple[int, ...], str]
, default:0
) –Padding of the convolution. Can be a single integer (shared along all spatial dimensions), an
N
-tuple of integers, or a string. Default:0
. Allowed strings are'same'
and'valid'
. -
stride
(Union[int, Tuple[int, ...]]
, default:1
) –Stride of the convolution. Can be a single integer (shared along all spatial dimensions), or an
N
-tuple of integers. Default:1
. -
simplify
(bool
, default:True
) –Whether to use a simplified einsum expression. Default:
True
.
Returns:
-
Tensor
–Unfolded input. Has shape
[batch_size, in_channels * tot_kernel_size, tot_output_size]
wheretot_kernel_size
is the kernel dimension product andtot_output_size
is the product of the output spatial dimensions. Ineinops
notation, the index structure isn (c_in k1 k2 ...) (o1 o2 ...)
.