KFLSTMModel¶
Core EKF-LSTM model components. Source: src/KFLSTMModel.py.
FeedForwardDecoder ¶
Bases: Module
Two-layer feedforward network used as the nonlinear observation function C(h). Maps the EKF state h to predicted sensor measurements y_h = C(h).
Architecture: Linear(input_size, hidden_size) -> ReLU -> Linear(hidden_size, output_size).
Source code in src/KFLSTMModel.py
LSTMModel ¶
Bases: Module
Single-layer (or multi-layer) LSTM with a linear output head and ReLU activation. Used as a sub-model within KFLSTMModel for state transition (model_f), process noise covariance (model_q), and measurement noise covariance (model_r).
Source code in src/KFLSTMModel.py
KFLSTMModel ¶
KFLSTMModel(is_f, is_q, is_r, os_f, os_q, os_r, hidden_size, num_layers, ff_hidden_size, lr, la_mse, la_nll_r, la_nll_h, la_CPCT_cov, la_K)
Bases: Module
LSTM-based Extended Kalman Filter (EKF) model for ICS time series anomaly detection.
Learns to predict the EKF residual r_t and innovation covariance S_t such that r_t ~ N(0, S_t). The model comprises four learned sub-models: - model_f: LSTM state transition function f(h) -> h_next - model_C: FeedForward observation function C(h) -> y_h - model_q: LSTM predicting lower-triangular process noise covariance Q_t - model_r: LSTM predicting lower-triangular measurement noise covariance R_t
Jacobians of model_f and model_C are computed via torch.autograd to linearize the nonlinear dynamics for the EKF covariance update equations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
is_f
|
int
|
State space dimension (SD). |
required |
is_q
|
int
|
Input dimension for the process noise LSTM (= is_f). |
required |
is_r
|
int
|
Measurement space dimension (MD). |
required |
os_f
|
int
|
Output dimension for the state transition LSTM (= is_f). |
required |
os_q
|
int
|
Number of lower-triangular elements in Q_t (= is_f*(is_f+1)//2). |
required |
os_r
|
int
|
Number of lower-triangular elements in R_t (= is_r*(is_r+1)//2). |
required |
hidden_size
|
int
|
LSTM hidden state size. |
required |
num_layers
|
int
|
Number of LSTM layers. |
required |
ff_hidden_size
|
int
|
Hidden size of the FeedForwardDecoder. |
required |
lr
|
float
|
Learning rate (stored for reference). |
required |
la_mse, la_nll_r, la_nll_h, la_CPCT_cov, la_K
|
float
|
Loss amplifier weights. |
required |
Source code in src/KFLSTMModel.py
computeKalmanCond ¶
Reshapes the Kalman gain to (SD x MD). Optionally computes and prints condition number diagnostics (singular values, spectral norms) for debugging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Kt
|
Tensor
|
Kalman gain K^T. |
required |
bsize
|
int
|
Batch size. |
required |
compute_stat
|
bool
|
If True, prints SVD-based condition number stats. |
False
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: Kalman gain reshaped as (SD x MD). |
Source code in src/KFLSTMModel.py
forward ¶
Primary EKF forward pass used in the zkSNARK prediction circuit.
Takes pre-computed Jacobians and Kalman gain as explicit circuit inputs so they can be committed to via Poseidon hashes. Runs one EKF update step and returns the updated state and all intermediate quantities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hin
|
Tensor
|
Current state estimate h_t (1 x 1 x SD). |
required |
yin
|
Tensor
|
Current sensor measurement y_t (1 x 1 x MD). |
required |
jac_fh
|
Tensor
|
Pre-computed Jacobian of f w.r.t. h (1 x SD x SD). |
required |
jac_Ch
|
Tensor
|
Pre-computed Jacobian of C w.r.t. h (1 x MD x SD). |
required |
P
|
Tensor
|
Current state error covariance (1 x SD x SD). |
required |
Kt
|
Tensor
|
Pre-computed Kalman gain K^T (1 x MD x SD). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
(h_out, out_h, dy, cov_h, cov_r, CPCT_cov, P_updated, kalman_check_status, kalman_diff, kalman_threshold) |
Source code in src/KFLSTMModel.py
forward_one_step ¶
Single EKF step with internally computed Jacobians. Used during non-ZKP inference and testing. Iterates over the batch dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hin
|
Tensor
|
Batch of state inputs (batch x 1 x SD). |
required |
yin
|
Tensor
|
Batch of measurement inputs (batch x 1 x MD). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
(h_out, delta_h, delta_y, cov_h, cov_r, CPCT_cov, K) all reshaped to (batch, ...). |
Source code in src/KFLSTMModel.py
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 391 392 393 394 395 396 397 398 399 400 401 402 | |
forward_train ¶
Batched EKF forward pass for training. Computes Jacobians in batch (detached for efficiency) and returns all quantities needed for loss_func().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hin
|
Tensor
|
Batch of state inputs (batch x 1 x SD). |
required |
yin
|
Tensor
|
Batch of measurement inputs (batch x 1 x MD). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
(h_out, out_h, dy, cov_h, cov_r, CPCT_cov, K2) all shaped (batch, ...). |
Source code in src/KFLSTMModel.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | |
forward_yield_jacobians ¶
Full EKF step that additionally returns the Jacobians and Kalman gain.
Called before zkSNARK circuit export to produce the jac_fh, jac_Ch, and Kt inputs that will be committed to in the prediction circuit. These are then passed as explicit inputs to forward() during witness generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hin
|
Tensor
|
Current state estimate (1 x 1 x SD). |
required |
yin
|
Tensor
|
Current measurement (1 x 1 x MD). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
(h_out, jac_fh, jac_Ch, P_updated, Kt) |
Source code in src/KFLSTMModel.py
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 | |
loss_func ¶
Composite training loss combining state prediction MSE with condition number penalties on the learned covariance matrices.
Loss = la_mse * MSE(h_pred, h_real) + la_nll_r * SSE(cond(R_t), 1) + la_nll_h * SSE(cond(Q_t), 1) + la_CPCT_cov * SSE(cond(CPCT_cov), 1) + la_K * SSE(cond(K_t), 1)
The condition number penalties encourage well-conditioned covariance matrices, improving numerical stability in both the EKF and the zkSNARK fixed-point arithmetic.
Returns:
| Type | Description |
|---|---|
|
Tuple[torch.Tensor, ...]: (total_loss, mse_loss, nll_loss_r, nll_loss_h) |
Source code in src/KFLSTMModel.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 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 | |
squared_exponential_kernel ¶
Computes the squared exponential kernel function between two sets of points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x1
|
Tensor
|
First set of points (shape: [N, D]) |
required |
x2
|
Tensor
|
Second set of points (shape: [M, D]) |
required |
lengthscale
|
Tensor
|
Lengthscale parameter (shape: [1] or [D]) |
required |
variance
|
Tensor
|
Variance parameter (shape: [1]) |
required |
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: Kernel matrix (shape: [N, M]) |