Physics-Informed RNN¶
Physics-Informed RNN models with dual encoder architecture.
PIRNN ¶
PIRNN(n_u: int, n_y: int, init_sz: int, prognosis: Module | None = None, n_y_supervised: int | None = None, n_x: int = 0, hidden_size: int = 100, rnn_layer: int = 1, state_encoder_hidden: int = 64, linear_layer: int = 1, final_layer: int = 0, init_diag_only: bool = False, default_encoder_mode: str = 'sequence', p_state_encoder: float = 0.0, init_sz_range: tuple[int, int] | None = None, **kwargs)
Bases: Module
Physics-Informed RNN with dual encoders: Sequence and State.
Uses a diagnosis model (sequence encoder) to estimate initial hidden state from an initialization window, then a prognosis model to predict forward. Alternatively, an MLP state encoder maps a single physical state to hidden state for faster initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_u
|
int
|
Number of inputs. |
required |
n_y
|
int
|
Number of outputs (total: supervised + auxiliary). |
required |
init_sz
|
int
|
Initialization sequence length. |
required |
prognosis
|
Module | None
|
stateful model returning |
None
|
n_y_supervised
|
int | None
|
Number of supervised outputs (in dataset). Defaults to n_y. |
None
|
n_x
|
int
|
Number of extra states. |
0
|
hidden_size
|
int
|
Hidden state size (for default prognosis and diagnosis). |
100
|
rnn_layer
|
int
|
Number of RNN layers (for default prognosis and diagnosis). |
1
|
state_encoder_hidden
|
int
|
Hidden size for state encoder MLP. |
64
|
linear_layer
|
int
|
Linear layers in diagnosis RNN. |
1
|
final_layer
|
int
|
Final layer complexity. |
0
|
init_diag_only
|
bool
|
Limit diagnosis to init_sz. |
False
|
default_encoder_mode
|
str
|
Default encoder mode during inference. |
'sequence'
|
p_state_encoder
|
float
|
Probability of using the state encoder per training batch. |
0.0
|
init_sz_range
|
tuple[int, int] | None
|
If set, randomize |
None
|
**kwargs
|
Additional arguments passed to default diagnosis RNN. |
{}
|
Source code in tsfast/pinn/pirnn.py
forward ¶
Forward pass with encoder mode auto-detection or explicit selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor [batch, seq, features]. |
required |
init_state
|
list | None
|
Initial hidden state. If None, estimated by encoder. |
None
|
encoder_mode
|
str
|
Encoder selection - 'none', 'sequence', or 'state'. |
'default'
|
Source code in tsfast/pinn/pirnn.py
encode_single_state ¶
Convert single physical state to flat hidden state vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
physical_state
|
Tensor
|
Physical state |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Flat hidden state |
Source code in tsfast/pinn/pirnn.py
AuxiliaryOutputLoss ¶
Wrapper that applies loss only to supervised outputs, ignoring auxiliary outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss_func
|
Callable
|
Loss function to wrap. |
required |
n_supervised
|
int
|
Number of supervised output channels. |
required |
Source code in tsfast/pinn/pirnn.py
__call__ ¶
Apply loss only to first n_supervised channels of predictions.
PIRNNLearner ¶
PIRNNLearner(dls, init_sz: int, n_aux_outputs: int = 0, attach_output: bool = False, loss_func: Callable = nn.L1Loss(), metrics: list | None = None, opt_func: Callable = torch.optim.Adam, lr: float = 0.003, transforms: list | None = None, augmentations: list | None = None, aux_losses: list | None = None, grad_clip: float | None = None, plot_fn: Callable | None = None, input_norm: type | None = StandardScaler, output_norm: type | None = None, prognosis: Module | None = None, hidden_size: int = 100, rnn_layer: int = 1, device: device | None = None, show_bar: bool = True, **kwargs) -> Learner
Create PIRNN learner with appropriate configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dls
|
DataLoaders. |
required | |
init_sz
|
int
|
Initialization sequence length. |
required |
n_aux_outputs
|
int
|
Number of auxiliary outputs (not in dataset). |
0
|
attach_output
|
bool
|
Whether to attach output to input via prediction_concat. |
False
|
loss_func
|
Callable
|
Loss function. |
L1Loss()
|
metrics
|
list | None
|
Metrics. |
None
|
opt_func
|
Callable
|
Optimizer. |
Adam
|
lr
|
float
|
Learning rate. |
0.003
|
transforms
|
list | None
|
Additional transforms (train + valid). |
None
|
augmentations
|
list | None
|
Additional augmentations (train only). |
None
|
aux_losses
|
list | None
|
Additional auxiliary losses. |
None
|
grad_clip
|
float | None
|
max gradient norm for clipping, or None to disable. |
None
|
plot_fn
|
Callable | None
|
plotting function for show_batch/show_results. |
None
|
input_norm
|
type | None
|
Input normalization Scaler class. |
StandardScaler
|
output_norm
|
type | None
|
Output denormalization Scaler class. |
None
|
prognosis
|
Module | None
|
Custom prognosis model (default: RNN with ret_full_hidden=True). |
None
|
hidden_size
|
int
|
Hidden units for default prognosis and diagnosis. |
100
|
rnn_layer
|
int
|
Number of RNN layers for default prognosis and diagnosis. |
1
|
device
|
device | None
|
target device (auto-detected if None). |
None
|
show_bar
|
bool
|
whether to show tqdm progress bars. |
True
|
**kwargs
|
Additional arguments for PIRNN. |
{}
|
Source code in tsfast/pinn/pirnn.py
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | |