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
A fixed (non-learnable) argument passed to an ODE/PDE function.
Wraps a float constant or a callable and provides a uniform
__call__ interface. See also Parameter for the learnable
variant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float | Callable[[Tensor], Tensor]
|
The value (float) or function (callable). |
required |
Example
beta = Argument(0.3) beta(torch.tensor([1.0])) tensor(0.3000) beta_fn = Argument(lambda t: 0.3 * torch.exp(-0.1 * t)) beta_fn(torch.tensor([0.0])) tensor([0.3000])
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. |
Example
coords = torch.linspace(0, 10, 100).unsqueeze(1) domain = Domain.from_x(coords) domain.x0, domain.x1 (0.0, 10.0)
Source code in src/anypinn/core/nn.py
Field
Bases: Module
A neural field mapping coordinates to a vector of state variables.
For an ODE this maps t -> [S, I, R]; for a PDE it maps
(x, t) -> u(x, t).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
MLPConfig
|
Configuration for the MLP backing this field. |
required |
Example
field = Field(MLPConfig( ... in_dim=1, out_dim=3, ... hidden_layers=[32, 32], ... activation="tanh", ... )) t = torch.rand(10, 1) field(t).shape torch.Size([10, 3])
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
A learnable parameter that participates in gradient optimization.
Supports scalar parameters (a single trainable value) or
function-valued parameters (e.g. beta(t)) backed by a small MLP.
Because Parameter is a subclass of Argument, it can be
used anywhere an Argument is expected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ScalarConfig | MLPConfig
|
Configuration for the parameter (ScalarConfig or MLPConfig). |
required |
Example
Scalar parameter starting at 0.3
beta = Parameter(ScalarConfig(init_value=0.3)) beta(torch.tensor([1.0])) # returns ~0.3
Function-valued parameter beta(t)
beta_t = Parameter(MLPConfig( ... in_dim=1, out_dim=1, ... hidden_layers=[8], ... activation="tanh", ... ))
Source code in src/anypinn/core/nn.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
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. |