A modular and robust PyTorch implementation of Lagrangian Neural Networks (LNNs) with Hessian-based regularization for improved training stability. This repository accompanies the paper:
Learning Relativistic Geodesics and Chaotic Dynamics via Stabilized Lagrangian Neural Networks
Lagrangian Neural Networks can learn arbitrary Lagrangians from trajectory data, but their unusual optimization objective leads to significant training instabilities. This implementation addresses these challenges through:
- Hessian Regularization: Penalizes unphysical signatures in the Lagrangian's second derivatives with respect to velocities, preventing the network from learning unstable dynamics
- Improved Activation Functions: GeLU activation functions better suited to learning Lagrangians
- Physics-Aware Coordinate Scaling: Improves stability across different physical systems
- Extended Training Bounds: Helps networks learn periodic boundary conditions
- Support for multiple physical systems:
- Classical Mechanics:
- Constant force motion
- Harmonic oscillator (1D spring)
- Spring pendulum (2D)
- Double pendulum (chaotic system)
- Triple pendulum (highly chaotic system)
- Differential Geometry:
- Sphere geodesic (free particle on a sphere)
- Relativistic Systems:
- AdS₂ geodesic (2D Anti-de Sitter spacetime)
- AdS₃ geodesic (3D Anti-de Sitter spacetime)
- AdS₄ geodesic (4D Anti-de Sitter spacetime)
- Classical Mechanics:
- Energy conservation through Lagrangian mechanics
- Trajectory visualizations with animations
- Comprehensive evaluation and analysis tools
Hessian Regularized LNN/
├── src/
│ ├── main.py # Main entry point
│ ├── analytical_tools.py # Analytical tools for evaluation
│ ├── hyperparameter_search.py # Hyperparameter search utilities
│ ├── architectures/ # Neural network architectures
│ │ ├── gelu_LNN.py # GeLU LNN architecture
│ │ ├── relativistic_LNN.py # Relativistic LNN with Lorentzian signature
│ │ └── ... # Other architectures
│ ├── physical_systems.py # Physical system implementations
│ ├── LNN.py # Base neural network architectures
│ ├── train.py # Training functions
│ ├── evaluate.py # Evaluation and plotting
│ ├── file_exports.py # Save/load functionality
│ ├── ode_solve.py # Numerical integration
│ ├── dataset_creation.py # Data generation
│ ├── lagrangian_demo.py # Solving by Lagrangian demonstration
│ └── variance_preserving_initializations.py # Custom initializations
├── outputs/ # Generated outputs and saved models
├── requirements.txt
├── .gitignore
└── README.md
- Python 3.11+
- CUDA-capable GPU (optional but recommended)
-
Clone the repository:
git clone https://github.com/abdullahumuth/hessian_regularized_lnn.git cd hessian_regularized_lnn -
Create a virtual environment:
python -m venv lnn-env source lnn-env/bin/activate # On Windows: lnn-env\Scripts\activate
-
Install PyTorch with CUDA support (adjust for your CUDA version):
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu126
-
Install remaining dependencies:
pip install -r requirements.txt
-
For video animations, install FFmpeg separately:
- Ubuntu/Debian:
sudo apt install ffmpeg - macOS:
brew install ffmpeg - Windows: Download from https://ffmpeg.org/
- Ubuntu/Debian:
| System | --physics flag |
DOF | Description |
|---|---|---|---|
| Constant Force | constant_force |
2 | Simple motion under constant force (e.g., gravity) |
| Harmonic Oscillator | harmonic_oscillator |
2 | 1D spring oscillator |
| Spring Pendulum | spring_pendulum |
4 | 2D pendulum with elastic spring |
| Double Pendulum | double_pendulum |
4 | Chaotic coupled pendulum system |
| Triple Pendulum | triple_pendulum |
6 | Highly chaotic three-mass pendulum |
| System | --physics flag |
DOF | Description |
|---|---|---|---|
| Sphere Geodesic | sphere_geodesic |
4 | Free particle constrained to sphere surface |
| System | --physics flag |
DOF | Description |
|---|---|---|---|
| AdS₂ Geodesic | ads2_geodesic |
4 | Geodesic in 2D Anti-de Sitter spacetime |
| AdS₃ Geodesic | ads3_geodesic |
6 | Geodesic in 3D Anti-de Sitter spacetime |
| AdS₄ Geodesic | ads4_geodesic |
8 | Geodesic in 4D Anti-de Sitter spacetime |
Important: For relativistic systems (those with spacetime coordinates as inputs), you must use
--model_name relativisticto enable the Lorentzian signature regularization. See Relativistic Systems for details.
cd src
python main.py [options]--physics SYSTEM- Physical system to use (see tables above)
--model_name NAME- Name of the model architecture to use:LNN(default) - Standard LNN with eigenvalue regularization for classical systemsrelativistic- LNN with Lorentzian signature regularization for relativistic systems
--mode MODE- Operation mode:train_from_scratch- Train a new model from scratchtrain_from_imported- Continue training from existing modeltest- Test existing model and generate visualizations
--load_path PATH- Path to load existing model/data from--save_path PATH- Path to save outputs (auto-generated if not specified)--load_history- Load existing training history--load_trajectory- Load existing trajectory data for evaluation--training_data_path PATH- Path to custom training dataset
--physical_params P1 P2 ...- Physical parameters in order:- Double pendulum:
[m1, m2, l1, l2, g] - Triple pendulum:
[m1, m2, m3, l1, l2, l3, g] - Harmonic oscillator:
[m, k] - Spring pendulum:
[m, k, g] - Constant force:
[m, g] - Sphere geodesic:
[m, R] - AdS geodesics:
[L](AdS length scale)
- Double pendulum:
--lr RATE- Learning rate (default: 1e-3)--num_samples N- Number of training samples--total_epochs N- Total training epochs--minibatch_size N- Minibatch size--eta_min RATE- Minimum learning rate for cosine annealing scheduler--lambda_val VALUE- Regularization strength for Hessian penalty (default: 5)
--num_layers N- Number of neural network layers--hidden_dim N- Hidden layer dimension
--train_position_bounds/--train_velocity_bounds- Training data bounds--validation_position_bounds/--validation_velocity_bounds- Validation bounds--test_position_bounds/--test_velocity_bounds- Test trajectory bounds
--num_tests N- Number of test trajectories to generate--time_step DT- Time step for integration--time_bounds START END- Time bounds for trajectory simulation
--dtype TYPE- Data type:float32orfloat64(default: float32)--device DEVICE- Device:cudaorcpu(default: cuda)--verbose- Enable verbose output--animate- Generate trajectory animations
--no_demo- Disable Lagrangian demo mode--no_debug- Disable debug evaluation mode--no_trajectory- Disable trajectory testing
# Train double pendulum with Hessian regularization
python main.py --physics double_pendulum --mode train_from_scratch --total_epochs 300 --lambda_val 5 --verbose
# Train triple pendulum (highly chaotic)
python main.py --physics triple_pendulum --mode train_from_scratch --total_epochs 500 --verbose
# Train spring pendulum
python main.py --physics spring_pendulum --mode train_from_scratch --total_epochs 500 --verbose
# Train sphere geodesic
python main.py --physics sphere_geodesic --mode train_from_scratch --total_epochs 50 --verbose
# Continue training from existing model
python main.py --physics double_pendulum --mode train_from_imported --load_path ./outputs/my_model --total_epochs 100For relativistic systems (those with spacetime coordinates as inputs), you must specify --model_name relativistic to use the appropriate Lorentzian signature regularization:
# Train AdS2 geodesic
python main.py --physics ads2_geodesic --model_name relativistic --mode train_from_scratch --total_epochs 120 --verbose
# Train AdS3 geodesic
python main.py --physics ads3_geodesic --model_name relativistic --mode train_from_scratch --total_epochs 150 --verbose
# Train AdS4 geodesic
python main.py --physics ads4_geodesic --model_name relativistic --mode train_from_scratch --total_epochs 200 --verbose# Test classical model
python main.py --physics double_pendulum --mode test --load_path ./outputs/double_pendulum_model --verbose
# Test relativistic model
python main.py --physics ads4_geodesic --model_name relativistic --mode test --load_path ./outputs/ads4_model --verbose
# Test with animations
python main.py --physics double_pendulum --mode test --load_path ./outputs/model --animate --verbose
# Quick test without debug or trajectory evaluation
python main.py --physics double_pendulum --mode test --load_path ./outputs/model --no_debug --no_trajectoryThe system generates outputs in directories with experiment-specific naming:
outputs/
└── {experiment_name}/
├── model.pt # Trained PyTorch model
├── particle.json # Physical system parameters and configuration
├── history.csv # Training loss history (epoch, train_loss, val_loss)
├── history_metadata.json # Training metadata and hyperparameters
├── debug_results/ # Debug and diagnostic outputs
├── test_results/ # Test evaluation results
├── trajectory_test_results/ # Numerical trajectory comparison data
└── validation_results/ # Validation analysis
Based on typical experimental configurations, the system supports various experiment naming patterns:
- Architecture Experiments:
GeLU/,Softplus/,xtanhkx/ - Initialization Experiments:
GeLUInitialized/,SoftplusInitialized/ - Hybrid Experiments:
QuadraticHybrid/,xtanhkxGeLUHybrid/ - Normalization Studies:
DyTNoNormalization/,LNNNoNormalization/ - Specialized Variants:
GeLUInitializedNoExtraTerm/,SoftplusNaiveInitialized/
- model.pt: Complete trained PyTorch model with all learned parameters
- particle.json: Serialized physical system containing all configuration and parameters
- history.csv: Training progress with loss values over epochs
- history_metadata.json: Complete training configuration, hyperparameters, and experiment settings
- debug_results/: Debug outputs, intermediate results, and diagnostic information
- test_results/: Comprehensive test evaluation including visualizations and error analysis
- trajectory_test_results/: Numerical trajectory data for comparison between analytical and predicted solutions
- validation_results/: Validation analysis and performance metrics
The primary source of training instability in LNNs stems from computing the inverse of the Lagrangian's Hessian with respect to generalized velocities (the mass matrix). When this matrix approaches singularity, training becomes unstable.
For classical systems, we penalize negative eigenvalues of the mass matrix:
This ensures the mass matrix remains positive semi-definite, which is physically required for stable classical dynamics.
For relativistic systems where spacetime coordinates are inputs, the mass matrix must have Lorentzian signature (one negative eigenvalue for the timelike direction). We enforce:
- The
$M_{00}$ component (timelike) must be non-positive - The spatial submatrix must be positive semi-definite
This allows LNNs to learn geodesic motion in curved spacetimes and extract metric tensor elements.
The --lambda_val parameter controls regularization strength (default: 5). Higher values enforce stricter constraints but may slow convergence.
LNNs learn the Lagrangian function
This ensures energy conservation and physical consistency. The acceleration is computed as:
The AdS geodesic systems model particle motion in Anti-de Sitter spacetime using the Poincaré patch metric:
These systems are relevant to the AdS/CFT correspondence in theoretical physics. Our Lorentzian regularization enables LNNs to learn such metrics directly from trajectory data.
Common issues and solutions:
- CUDA out of memory during training: Reduce
--minibatch_sizeor--num_samples - CUDA out of memory during ODE solving: This happens during trajectory testing. Increase time splitting with the
num_time_splitsparameter in the code, or reduce the number of test cases - Animation failures: Install FFmpeg or check file permissions
- Training instability: Increase
--lambda_valfor stronger regularization or lower learning rate with--lr - Import errors: Ensure all requirements are installed:
pip install -r requirements.txt - Long trajectory computation: The system automatically uses time-splitting for memory management. Check console output for progress
- Model loading errors: Ensure the model path exists and contains both
model.ptandparticle.json - Relativistic system not training properly: Ensure you're using
--model_name relativisticfor systems with spacetime inputs - Coordinate singularity issues: Some systems (e.g., sphere geodesic at poles, AdS at z=0) have coordinate singularities; ensure training bounds avoid these regions
If you use this code in your research, please cite the following paper:
We are preparing a formal citation for the paper. Please check back later for the updated citation information.
This project is licensed under the Apache 2.0 license.
For questions or issues, please open a GitHub issue or contact me at abdullah.hamzaogullari@std.bogazici.edu.tr