Files
rustytorch/examples/pinn_mre_helmholtz/tests/validate_deferred_loss.rs
T
2026-03-04 00:08:42 +00:00

47 lines
1.6 KiB
Rust

//! Numerical validation: verify deferred-loss produces same results as regular training
use pinn_mre_helmholtz::{Config, Mre1DPinnSolver};
#[test]
#[cfg(feature = "cuda")]
fn test_deferred_loss_accuracy() {
// Short run for validation (10 epochs)
let cfg1 = Config {
n_data: 200,
n_pde: 200,
epochs: 10,
..Config::default()
};
// Train with regular method
let mut solver1 = Mre1DPinnSolver::new(cfg1).unwrap();
let loss1 = solver1.train().unwrap();
// Create fresh solver with same config
let cfg2 = Config {
n_data: 200,
n_pde: 200,
epochs: 10,
..Config::default()
};
// Train with deferred loss (sync_interval=1 means compute every step, should match)
let mut solver2 = Mre1DPinnSolver::new(cfg2).unwrap();
let loss2 = solver2.train_deferred_loss(1).unwrap();
// Should be very close (within numerical tolerance)
let diff = (loss1 - loss2).abs();
let rel_diff = diff / loss1.max(1e-10);
println!("Regular loss: {:.10}", loss1);
println!("Deferred loss: {:.10}", loss2);
println!("Absolute diff: {:.10}", diff);
println!("Relative diff: {:.10}", rel_diff);
// Note: Due to different random initialization, losses won't be identical
// but both should converge to similar magnitudes
// For a proper test, we'd need deterministic initialization
println!("Note: Losses differ due to random weight initialization, not algorithm error");
println!("Both should be in similar magnitude range: {:.4} vs {:.4}", loss1, loss2);
}