Modules
einconv.modules.ConvNd
ConvNd(N: int, in_channels: int, out_channels: int, kernel_size: Union[int, Tuple[int, ...]], stride: Union[int, Tuple[int, ...]] = 1, padding: Union[int, Tuple[int, ...], str] = 0, dilation: Union[int, Tuple[int, ...]] = 1, groups: int = 1, bias: bool = True, padding_mode: str = 'zeros', device: Union[None, torch.device] = None, dtype: Union[None, torch.dtype] = None, simplify: bool = True)
PyTorch module for N-dimensional convolution that uses einsum.
Initialize N-dimensional convolution layer.
Parameters are initialized using the same convention as PyTorch's convolutions.
The weight has shape [out_channels, in_channels // groups, *kernel_size]
with len(kernel_size) == N. The bias has shape [out_channels].
Parameters:
-
N(int) –Convolution dimension. For
N=1,2,3the layer behaves like PyTorch'snn.Conv{N=1,2,3}d. However, it also works forN>3. -
in_channels(int) –Number of input channels.
-
out_channels(int) –Number of output channels.
-
kernel_size(Union[int, Tuple[int, ...]]) –Kernel dimensions. Can be a single integer (shared along all spatial dimensions), or an
N-tuple of integers. -
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, 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'. -
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. -
bias(bool, default:True) –Whether to use a bias. Default:
True. -
padding_mode(str, default:'zeros') –How to perform padding. Default:
'zeros'. No other modes are supported at the moment. -
device(Union[None, device], default:None) –Device on which the module is initialized.
-
dtype(Union[None, dtype], default:None) –Data type assumed by the module.
-
simplify(bool, default:True) –Whether to use a simplified einsum expression. Default:
True.
Raises:
-
NotImplementedError–For unsupported padding modes.
-
ValueError–For invalid combinations of
in_channels,out_channels, andgroups.
Source code in einconv/modules/conv.py
forward
Perform convolution on the input.
Parameters:
-
x(Tensor) –Convolution input. Has shape
[batch_size, in_channels, *input_sizes]wherelen(input_sizes) == N.
Returns:
-
Tensor–Result of the convolution. Has shape
[batch_size, out_channels, *output_sizes]wherelen(output_sizes) == N. Ineinopsnotation, the index structure isn (g c_out) o1 o2 ....
Source code in einconv/modules/conv.py
from_nn_Conv
classmethod
Convert a torch.nn.Conv{1,2,3}d module to a ConvNd layer.
Parameters:
-
conv_module(Union[Conv1d, Conv2d, Conv3d]) –Convolution module.
-
simplify(bool, default:True) –Whether to use a simplified einsum expression. Default:
True.
Returns:
-
ConvNd–Converted ConvNd module.
Source code in einconv/modules/conv.py
einconv.modules.UnfoldNd
UnfoldNd(kernel_size: Union[int, Tuple[int, ...]], dilation: Union[int, Tuple[int, ...]] = 1, padding: Union[int, Tuple[int, ...]] = 0, stride: Union[int, Tuple[int, ...]] = 1, simplify: bool = True)
PyTorch module for N-dimensional input unfolding (im2col) that uses einsum.
Extracts sliding local blocks from a batched input tensor (im2col).
This module accepts batched tensors with N spatial dimensions. It acts like
torch.nn.Unfold for a 4d input (batched images), but works for arbitrary
N. See https://pytorch.org/docs/stable/generated/torch.nn.Unfold.html.
Parameters:
-
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, ...]], 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.
Source code in einconv/modules/unfold.py
forward
Compute the unfolded input.
Parameters:
-
x(Tensor) –Convolution input. Has shape
[batch_size, in_channels, *input_sizes]wherelen(input_sizes) == N.
Returns:
-
Tensor–Unfolded input. Has shape
[batch_size, in_channels * tot_kernel_size, tot_output_size]wheretot_kernel_sizeis the kernel dimension product andtot_output_sizeis the product of the output spatial dimensions. Ineinopsnotation, the index structure isn (c_in k1 k2 ...) (o1 o2 ...).