anypinn.core.nn
Neural network primitives and building blocks for PINN.
ArgsRegistry: TypeAlias = dict[str, Argument]
module-attribute
FieldsRegistry: TypeAlias = dict[str, Field]
module-attribute
ParamsRegistry: TypeAlias = dict[str, Parameter]
module-attribute
Argument
Represents an argument that can be passed to an ODE/PDE function. Can be a fixed float value or a callable function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float | Callable[[Tensor], Tensor]
|
The value (float) or function (callable). |
required |
Source code in src/anypinn/core/nn.py
__call__(x: Tensor) -> Tensor
Evaluate the argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor (context). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The value of the argument, broadcasted if necessary. |
Source code in src/anypinn/core/nn.py
__init__(value: float | Callable[[Tensor], Tensor])
Domain
dataclass
N-dimensional rectangular domain.
Attributes:
| Name | Type | Description |
|---|---|---|
bounds |
list[tuple[float, float]]
|
Per-dimension (min, max) pairs. |
dx |
list[float] | None
|
Per-dimension step size ( |
Source code in src/anypinn/core/nn.py
bounds: list[tuple[float, float]]
instance-attribute
dx: list[float] | None = None
class-attribute
instance-attribute
ndim: int
property
Number of spatial dimensions.
x0: float
property
Lower bound of the first dimension (convenience for 1-D / time-axis access).
x1: float
property
Upper bound of the first dimension.
__init__(bounds: list[tuple[float, float]], dx: list[float] | None = None) -> None
__repr__() -> str
from_x(x: Tensor) -> Domain
classmethod
Infer domain bounds and step sizes from a coordinate tensor of shape (N, d).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Coordinate tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Domain
|
Domain with bounds and dx inferred from the data. |
Source code in src/anypinn/core/nn.py
Field
Bases: Module
A neural field mapping coordinates -> vector of state variables. Example (ODE): t -> [S, I, R].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
MLPConfig
|
Configuration for the MLP backing this field. |
required |
Source code in src/anypinn/core/nn.py
encoder: nn.Module | None = encode
instance-attribute
net = nn.Sequential(*layers)
instance-attribute
__init__(config: MLPConfig)
Source code in src/anypinn/core/nn.py
forward(x: Tensor) -> Tensor
Forward pass of the field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input coordinates (e.g. time, space). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The values of the field at input coordinates. |
Source code in src/anypinn/core/nn.py
Parameter
Bases: Module, Argument
Learnable parameter. Supports scalar or function-valued parameter. For function-valued parameters (e.g. β(t)), uses a small MLP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ScalarConfig | MLPConfig
|
Configuration for the parameter (ScalarConfig or MLPConfig). |
required |
Source code in src/anypinn/core/nn.py
config = config
instance-attribute
mode: Literal['scalar', 'mlp']
property
Mode of the parameter: 'scalar' or 'mlp'.
net = nn.Sequential(*layers)
instance-attribute
value = nn.Parameter(torch.tensor(float(config.init_value), dtype=(torch.float32)))
instance-attribute
__init__(config: ScalarConfig | MLPConfig)
Source code in src/anypinn/core/nn.py
forward(x: Tensor | None = None) -> Tensor
Get the value of the parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor | None
|
Input tensor (required for 'mlp' mode). |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
The parameter value. |
Source code in src/anypinn/core/nn.py
build_criterion(name: Criteria) -> nn.Module
Return the loss-criterion module for the given name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Criteria
|
One of |
required |
Returns:
| Type | Description |
|---|---|
Module
|
The corresponding PyTorch loss module. |
Source code in src/anypinn/core/nn.py
get_activation(name: Activations) -> nn.Module
Get the activation function module by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Activations
|
The name of the activation function. |
required |
Returns:
| Type | Description |
|---|---|
Module
|
The PyTorch activation module. |