Continuous convolution#
- class ContinuousConvBlock(input_numb_field, output_numb_field, filter_dim, stride, model=None, optimize=False, no_overlap=False)[source]
Bases:
BaseContinuousConv
Implementation of Continuous Convolutional operator.
The algorithm expects input to be in the form: \([B, N_{in}, N, D]\) where \(B\) is the batch_size, \(N_{in}\) is the number of input fields, \(N\) the number of points in the mesh, \(D\) the dimension of the problem. In particular:
\(D\) is the number of spatial variables + 1. The last column must contain the field value. For example for 2D problems \(D=3\) and the tensor will be something like
[first coordinate, second coordinate, field value]
.\(N_{in}\) represents the number of vectorial function presented. For example a vectorial function \(f = [f_1, f_2]\) will have \(N_{in}=2\).
See also
Original reference: Coscia, D., Meneghetti, L., Demo, N. et al. A continuous convolutional trainable filter for modelling unstructured data. Comput Mech 72, 253–265 (2023). DOI https://doi.org/10.1007/s00466-023-02291-1
- Parameters:
input_numb_field (int) – Number of fields \(N_{in}\) in the input.
output_numb_field (int) – Number of fields \(N_{out}\) in the output.
filter_dim (tuple(int) | list(int)) – Dimension of the filter.
stride (dict) – Stride for the filter.
model (torch.nn.Module) – Neural network for inner parametrization, defaults to
None
. If None, a default multilayer perceptron of width three and size twenty with ReLU activation is used.optimize (bool) – Flag for performing optimization on the continuous filter, defaults to False. The flag optimize=True should be used only when the scatter datapoints are fixed through the training. If torch model is in
.eval()
mode, the flag is automatically set to False always.no_overlap (bool) – Flag for performing optimization on the transpose continuous filter, defaults to False. The flag set to True should be used only when the filter positions do not overlap for different strides. RuntimeError will raise in case of non-compatible strides.
Note
Using optimize=True the filter can be use either in forward or in transpose mode, not both. If optimize=False the same filter can be used for both transpose and forward modes.
- Example:
>>> class MLP(torch.nn.Module): def __init__(self) -> None: super().__init__() self. model = torch.nn.Sequential( torch.nn.Linear(2, 8), torch.nn.ReLU(), torch.nn.Linear(8, 8), torch.nn.ReLU(), torch.nn.Linear(8, 1)) def forward(self, x): return self.model(x) >>> dim = [3, 3] >>> stride = {"domain": [10, 10], "start": [0, 0], "jumps": [3, 3], "direction": [1, 1.]} >>> conv = ContinuousConv2D(1, 2, dim, stride, MLP) >>> conv ContinuousConv2D( (_net): ModuleList( (0): MLP( (model): Sequential( (0): Linear(in_features=2, out_features=8, bias=True) (1): ReLU() (2): Linear(in_features=8, out_features=8, bias=True) (3): ReLU() (4): Linear(in_features=8, out_features=1, bias=True) ) ) (1): MLP( (model): Sequential( (0): Linear(in_features=2, out_features=8, bias=True) (1): ReLU() (2): Linear(in_features=8, out_features=8, bias=True) (3): ReLU() (4): Linear(in_features=8, out_features=1, bias=True) ) ) ) )
- forward(X)[source]
Forward pass in the convolutional layer.
- Parameters:
x (torch.Tensor) – Input data for the convolution \([B, N_{in}, N, D]\).
- Returns:
Convolution output \([B, N_{out}, N, D]\).
- Return type:
- transpose_no_overlap(integrals, X)[source]
Transpose pass in the layer for no-overlapping filters
- Parameters:
integrals – Weights for the transpose convolution. Shape \([B, N_{in}, N]\) where B is the batch_size, :math`N_{in}` is the number of input fields, \(N\) the number of points in the mesh, D the dimension of the problem.
X (torch.Tensor) – Input data. Expect tensor of shape \([B, N_{in}, M, D]\) where \(B\) is the batch_size, :math`N_{in}`is the number of input fields, \(M\) the number of points in the mesh, \(D\) the dimension of the problem.
- Returns:
Feed forward transpose convolution. Tensor of shape \([B, N_{out}, M, D]\) where \(B\) is the batch_size, :math`N_{out}`is the number of input fields, \(M\) the number of points in the mesh, \(D\) the dimension of the problem.
- Return type:
Note
This function is automatically called when
.transpose()
method is used andno_overlap=True
- transpose_overlap(integrals, X)[source]
Transpose pass in the layer for overlapping filters
- Parameters:
integrals – Weights for the transpose convolution. Shape \([B, N_{in}, N]\) where B is the batch_size, :math`N_{in}` is the number of input fields, \(N\) the number of points in the mesh, D the dimension of the problem.
X (torch.Tensor) – Input data. Expect tensor of shape \([B, N_{in}, M, D]\) where \(B\) is the batch_size, :math`N_{in}`is the number of input fields, \(M\) the number of points in the mesh, \(D\) the dimension of the problem.
- Returns:
Feed forward transpose convolution. Tensor of shape \([B, N_{out}, M, D]\) where \(B\) is the batch_size, :math`N_{out}`is the number of input fields, \(M\) the number of points in the mesh, \(D\) the dimension of the problem.
- Return type:
Note
This function is automatically called when
.transpose()
method is used andno_overlap=False