scimba_torch.approximation_space.nn_space

Defines the neural network approximation space and its components.

Classes

NNxSpace(nb_unknowns, nb_parameters, ...)

A nonlinear approximation space using a neural network model.

NNxSpaceSplit(nb_unknowns, nb_parameters, ...)

A nonlinear approximation space using a neural network split into components.

NNxtSpace(nb_unknowns, nb_parameters, ...)

A nonlinear approximation space using a neural network model for space-time data.

NNxvSpace(nb_unknowns, nb_parameters, ...)

A nonlinear approximation space using a network model for phase space data.

SeparatedNNxSpace(nb_unknowns, ...)

A nonlinear approximation space using a neural network model with components.

class NNxSpace(nb_unknowns, nb_parameters, space_type, spatial_domain, integrator, **kwargs)[source]

Bases: AbstractApproxSpace, Module

A nonlinear approximation space using a neural network model.

This class represents a parametric approximation space, where the solution is modeled by a neural network. It integrates functionality for evaluating the network, setting/retrieving degrees of freedom, and computing the Jacobian.

Parameters:
  • nb_unknowns (int) – Number of unknowns in the approximation problem.

  • nb_parameters (int) – Number of parameters in the input.

  • space_type (Module) – The neural network class used to approximate the solution.

  • spatial_domain (VolumetricDomain) – The spatial domain of the problem.

  • integrator (TensorizedSampler) – Sampler used for integration over the spatial and parameter domains.

  • **kwargs – Additional arguments passed to the neural network model.

Raises:

KeyError – If parameters_bounds is not provided when using PeriodicMLP.

in_size: int

Size of the input to the neural network (spatial dimension + parameters).

out_size: int

Size of the output of the neural network (number of unknowns).

spatial_domain: VolumetricDomain

The spatial domain of the problem.

integrator: TensorizedSampler

Integrator combining the spatial and parameter domains.

type_space: str

Type of the approximation space.

pre_processing: Callable

Function to pre-process inputs.

post_processing: Callable

Function to post-process outputs.

network: torch.nn.Module

Neural network used for the approximation.

ndof: int

Total number of degrees of freedom in the network.

forward(features, with_last_layer=True)[source]

Evaluates the parametric model for given input features.

Parameters:
  • features (Tensor) – Input tensor with concatenated spatial and parameter data.

  • with_last_layer (bool) – Whether to include the last layer in evaluation. (Default value = True)

Returns:

Output tensor from the neural network.

Return type:

torch.Tensor

evaluate(x, mu, with_last_layer=True)[source]

Evaluates the parametric model for given inputs and parameters.

Parameters:
  • x (LabelTensor) – Input tensor from the spatial domain.

  • mu (LabelTensor) – Input tensor from the parameter domain.

  • with_last_layer (bool) – Whether to include the last layer in evaluation. (Default value = True)

Returns:

Output tensor from the neural network,

wrapped with multi-label metadata.

Return type:

MultiLabelTensor

set_dof(theta, flag_scope='all')[source]

Sets the degrees of freedom (DoF) for the neural network.

Parameters:
  • theta (Tensor) – A vector containing the network parameters.

  • flag_scope (str) – Scope flag for setting degrees of freedom. (Default value = “all”)

Return type:

None

get_dof(flag_scope='all', flag_format='list')[source]

Retrieves the degrees of freedom (DoF) of the neural network.

Parameters:
  • flag_scope (str) – Specifies the parameters to return. (Default value = “all”)

  • flag_format (str) – The format for returning the parameters. (Default value = “list”)

Return type:

Tensor

Returns:

The network parameters in the specified format.

jacobian(x, mu)[source]

Computes the Jacobian of the network with respect to its parameters.

Parameters:
  • x (LabelTensor) – Input tensor from the spatial domain.

  • mu (LabelTensor) – Input tensor from the parameter domain.

Return type:

Tensor

Returns:

Jacobian matrix of shape (num_samples, out_size, num_params).

class NNxSpaceSplit(nb_unknowns, nb_parameters, net_type, spatial_domain, integrator, **kwargs)[source]

Bases: AbstractApproxSpace, Module

A nonlinear approximation space using a neural network split into components.

It is designed to handle problems where the input space can be split into spatial and parameter components, allowing for more efficient processing via separate neural networks for each component.

Parameters:
  • nb_unknowns (int) – The number of unknowns in the approximation problem.

  • nb_parameters (int) – The number of parameters in the problem.

  • net_type (Module) – The neural network model used to approximate the solution.

  • spatial_domain (VolumetricDomain) – The domain representing the spatial component of the problem.

  • integrator (TensorizedSampler) – The sampler used for integration over the spatial domain.

  • **kwargs – Additional keyword arguments for configuring the neural network model.

Raises:
  • ValueError – If the network type is not supported.

  • KeyError – If parameters_bounds is not provided when using PeriodicMLP.

in_size: int

The size of the inputs to the neural network.

out_size: int

The size of the outputs from the neural network.

spatial_domain: VolumetricDomain

The spatial domain of the problem.

integrator: TensorizedSampler

The integrator for the spatial and parameter domains.

network_x: torch.nn.Module

The neural network for processing spatial inputs.

network_mu: torch.nn.Module

The neural network for processing parameter inputs.

network_cat: torch.nn.Module

The neural network for processing concatenated inputs.

ndof: int

The number of degrees of freedom (DoF) in the neural network.

forward(x, mu, with_last_layer=True)[source]

Evaluates the parametric model for given inputs and parameters.

Parameters:
  • x (Tensor) – Input tensor from the spatial domain.

  • mu (Tensor) – Input tensor from the parameter domain.

  • with_last_layer (bool) – Whether to include the last layer in evaluation. (Default value = True)

Returns:

Output tensor from the neural network.

evaluate(x, mu, with_last_layer=True)[source]

Evaluates the parametric model for given inputs and parameters.

Parameters:
  • x (LabelTensor) – Input tensor from the spatial domain.

  • mu (LabelTensor) – Input tensor from the parameter domain.

  • with_last_layer (bool) – Whether to include the last layer in evaluation. (Default value = True)

Returns:

The result of the neural network evaluation.

Return type:

MultiLabelTensor

set_dof(theta, flag_scope='all')[source]

Sets the degrees of freedom (DoF) for the neural network.

Parameters:
  • theta (Tensor) – A vector containing the network parameters.

  • flag_scope (str) – Scope flag for setting degrees of freedom. (Default value = “all”)

Return type:

None

get_dof(flag_scope='all', flag_format='list')[source]

Retrieves the degrees of freedom (DoF) of the neural network.

Parameters:
  • flag_scope (str) – Specifies the parameters to return. (Default value = “all”)

  • flag_format (str) – The format for returning the parameters. (Default value = “list”)

Return type:

Tensor

Returns:

The network parameters in the specified format.

jacobian(x, mu)[source]

Computes the Jacobian matrix of the model with respect to its inputs.

Parameters:
  • x (LabelTensor) – The input tensor for the spatial domain.

  • mu (LabelTensor) – The input tensor for the parameter domain.

Return type:

Tensor

Returns:

The Jacobian matrix of the model.

class SeparatedNNxSpace(nb_unknowns, nb_parameters, rank, net_type, spatial_domain, integrator, **kwargs)[source]

Bases: AbstractApproxSpace, Module

A nonlinear approximation space using a neural network model with components.

This class represents a parametric approximation space, where the solution is modeled by a neural network with separated components for efficient computation.

Parameters:
  • nb_unknowns (int) – Number of unknowns in the approximation problem.

  • nb_parameters (int) – Number of parameters in the input.

  • rank (int) – Rank of the separated tensor structure.

  • net_type (Module) – The neural network class used to approximate the solution.

  • spatial_domain (VolumetricDomain) – The spatial domain of the problem.

  • integrator (TensorizedSampler) – Sampler used for integration over the spatial and parameter domains.

  • **kwargs – Additional arguments passed to the neural network model.

Raises:

ValueError – If the network type is not supported.

in_size: int

Size of the input to the neural network (spatial dimension + parameters).

out_size: int

Size of the output of the neural network (number of unknowns).

spatial_domain: VolumetricDomain

The spatial domain of the problem.

integrator: TensorizedSampler

Integrator for sampling over the spatial and parameter domains.

network: nn.ModuleList

list of neural network modules used for the approximation.

ndof: int

Total number of degrees of freedom in the network.

forward(features)[source]

Evaluates the parametric model for given input features.

Parameters:

features (Tensor) – Input tensor with concatenated spatial and parameter data.

Return type:

Tensor

Returns:

Output tensor from the neural network.

evaluate(x, mu, with_last_layer=True)[source]

Evaluates the parametric model for given inputs and parameters.

Parameters:
  • x (LabelTensor) – Input tensor from the spatial domain.

  • mu (LabelTensor) – Input tensor from the parameter domain.

  • with_last_layer (bool) – Whether to include the last layer in evaluation. (Default value = True)

Return type:

MultiLabelTensor

Returns:

Output tensor from the neural network, wrapped with multi-label metadata.

Raises:

ValueError – If with_last_layer is False.

set_dof(theta, flag_scope='all')[source]

Sets the degrees of freedom (DoF) for the neural network.

Parameters:
  • theta (Tensor) – A vector containing the network parameters.

  • flag_scope (str) – Scope flag for setting degrees of freedom. (Default value = “all”)

Raises:

ValueError – If the flag_scope is not “all”.

Return type:

None

get_dof(flag_scope='all', flag_format='list')[source]

Retrieves the degrees of freedom (DoF) of the neural network.

Parameters:
  • flag_scope (str) – Specifies the parameters to return. (Default value = “all”)

  • flag_format (str) – The format for returning the parameters. (Default value = “list”)

Return type:

list | Tensor

Returns:

The network parameters in the specified format.

Raises:

ValueError – If the flag_scope is not “all” or If the flag_format is not “list” or “tensor”.

jacobian(x, mu)[source]

Computes the Jacobian of the network with respect to its parameters.

Parameters:
  • x (LabelTensor) – Input tensor from the spatial domain.

  • mu (LabelTensor) – Input tensor from the parameter domain.

Return type:

Tensor

Returns:

Jacobian matrix of shape (num_samples, out_size, num_params).

class NNxtSpace(nb_unknowns, nb_parameters, net_type, spatial_domain, integrator, **kwargs)[source]

Bases: AbstractApproxSpace, Module

A nonlinear approximation space using a neural network model for space-time data.

It represents a parametric approximation space, where the solution is modeled by a neural network, integrating functionality for evaluating the network, setting/retrieving degrees of freedom, and computing the Jacobian.

Parameters:
  • nb_unknowns (int) – Number of unknowns in the approximation problem.

  • nb_parameters (int) – Number of parameters in the input.

  • net_type (Module) – The neural network class used to approximate the solution.

  • spatial_domain (VolumetricDomain) – The spatial domain of the problem.

  • integrator (TensorizedSampler) – Sampler used for integration over the spatial and parameter domains.

  • **kwargs – Additional arguments passed to the neural network model.

Raises:

ValueError – If the network type is not supported.

in_size: int

Size of the input to the neural network (spatial dimension + parameters + time).

out_size: int

Size of the output of the neural network (number of unknowns).

spatial_domain: VolumetricDomain

The spatial domain of the problem.

integrator: TensorizedSampler

Integrator combining the spatial and parameter domains.

network: torch.nn.Module

Neural network used for the approximation.

ndof: int

Total number of degrees of freedom in the network.

forward(features, with_last_layer=True)[source]

Evaluates the parametric model for given input features.

Parameters:
  • features (Tensor) – Input tensor with concatenated spatial and parameter data.

  • with_last_layer (bool) – Whether to include the last layer in evaluation. (Default value = True)

Return type:

Tensor

Returns:

Output tensor from the neural network.

evaluate(t, x, mu, with_last_layer=True)[source]

Evaluates the parametric model for given inputs and parameters.

Parameters:
  • t (LabelTensor) – Input tensor from the time domain.

  • x (LabelTensor) – Input tensor from the spatial domain.

  • mu (LabelTensor) – Input tensor from the parameter domain.

  • with_last_layer (bool) – Whether to include the last layer in evaluation. (Default value = True)

Return type:

MultiLabelTensor

Returns:

Output tensor from the neural network, wrapped with multi-label metadata.

set_dof(theta, flag_scope='all')[source]

Sets the degrees of freedom (DoF) for the neural network.

Parameters:
  • theta (Tensor) – A vector containing the network parameters.

  • flag_scope (str) – Scope flag for setting degrees of freedom. (Default value = “all”)

Return type:

None

get_dof(flag_scope='all', flag_format='list')[source]

Retrieves the degrees of freedom (DoF) of the neural network.

Parameters:
  • flag_scope (str) – Specifies the parameters to return. (Default value = “all”)

  • flag_format (str) – The format for returning the parameters. (Default value = “list”)

Returns:

The network parameters in the specified format.

Return type:

torch.Tensor

jacobian(t, x, mu)[source]

Computes the Jacobian of the network with respect to its parameters.

Parameters:
  • t (LabelTensor) – Input tensor from the time domain.

  • x (LabelTensor) – Input tensor from the spatial domain.

  • mu (LabelTensor) – Input tensor from the parameter domain.

Return type:

Tensor

Returns:

Jacobian matrix of shape (num_samples, out_size, num_params).

class NNxvSpace(nb_unknowns, nb_parameters, net_type, spatial_domain, velocity_domain, integrator, **kwargs)[source]

Bases: AbstractApproxSpace, Module

A nonlinear approximation space using a network model for phase space data.

This class represents a parametric approximation space, where the solution is modeled by a neural network, integrating functionality for evaluating the network, setting/retrieving degrees of freedom, and computing the Jacobian.

Parameters:
  • nb_unknowns (int) – Number of unknowns in the approximation problem.

  • nb_parameters (int) – Number of parameters in the input.

  • net_type (Module) – The neural network class used to approximate the solution.

  • spatial_domain (VolumetricDomain) – The spatial domain of the problem.

  • velocity_domain (SurfacicDomain) – The velocity domain of the problem.

  • integrator (TensorizedSampler) – Sampler used for integration over the spatial and parameter domains.

  • **kwargs – Additional arguments passed to the neural network model.

Raises:
  • KeyError – If parameters_bounds is not provided when using PeriodicMLP or PeriodicResNet.

  • ValueError – If the network type is not supported.

in_size: int

Size of the input to the neural network (spatial dimension + velocity dimension + parameters).

out_size: int

Size of the output of the neural network (number of unknowns).

spatial_domain: VolumetricDomain

The spatial domain of the problem.

velocity_domain: SurfacicDomain

The velocity domain of the problem.

integrator: TensorizedSampler

Sampler used for integration over the spatial and parameter domains.

ndof: int

Total number of degrees of freedom in the network.

forward(features, with_last_layer=True)[source]

Evaluates the parametric model for given input features.

Parameters:
  • features (Tensor) – Input tensor with concatenated spatial and parameter data.

  • with_last_layer (bool) – Whether to include the last layer in evaluation. (Default value = True)

Return type:

Tensor

Returns:

Output tensor from the neural network.

evaluate(x, v, mu, with_last_layer=True)[source]

Evaluates the parametric model for given inputs and parameters.

Parameters:
  • x (LabelTensor) – Input tensor from the spatial domain.

  • v (LabelTensor) – Input tensor from the velocities domain.

  • mu (LabelTensor) – Input tensor from the parameter domain.

  • with_last_layer (bool) – Whether to include the last layer in evaluation. (Default value = True)

Return type:

MultiLabelTensor

Returns:

Output tensor from the neural network, wrapped with multi-label metadata.

set_dof(theta, flag_scope='all')[source]

Sets the degrees of freedom (DoF) for the neural network.

Parameters:
  • theta (Tensor) – A vector containing the network parameters.

  • flag_scope (str) – Scope flag for setting degrees of freedom. (Default value = “all”)

Return type:

None

get_dof(flag_scope='all', flag_format='list')[source]

Retrieves the degrees of freedom (DoF) of the neural network.

Parameters:
  • flag_scope (str) – Specifies the parameters to return. (Default value = “all”)

  • flag_format (str) – The format for returning the parameters. (Default value = “list”)

Return type:

Tensor

Returns:

The network parameters in the specified format.

jacobian(x, v, mu)[source]

Computes the Jacobian of the network with respect to its parameters.

Parameters:
  • x (LabelTensor) – Input tensor from the spatial domain.

  • v (LabelTensor) – Input tensor from the velocities domain.

  • mu (LabelTensor) – Input tensor from the parameter domain.

Return type:

Tensor

Returns:

Jacobian matrix of shape (num_samples, out_size, num_params).

expand_hidden_layers(layer_sizes, set_to_zero=True)[source]

Expands the hidden layers of the neural network to accommodate new sizes.

Parameters:
  • layer_sizes (list[int]) – list of sizes for the hidden layers.

  • set_to_zero (bool) – If True, initializes the new layers to zero. Otherwise, uses small random numbers. Default is True.

Return type:

None