refactor(hamiltonian-nn): modularize code, add tests, improve energy conservation#32
Open
olaropenclaw wants to merge 1 commit intoqbeer:masterfrom
Open
refactor(hamiltonian-nn): modularize code, add tests, improve energy conservation#32olaropenclaw wants to merge 1 commit intoqbeer:masterfrom
olaropenclaw wants to merge 1 commit intoqbeer:masterfrom
Conversation
…rgy conservation - Modularize architecture: split into mlp.py, hnn.py, training.py, integrators.py - Add symplectic leapfrog integrator for better energy conservation - Add comprehensive test suite (18 tests covering data gen, models, integrators, training) - Make MLP configurable with flexible hidden dimensions - Add docstrings and type hints throughout - Improve README with explanation of energy oscillation in original code - Add CLI arguments to train.py for reproducibility and configuration - Fix phi range in data generation (0 to 2π instead of 0 to π) The energy oscillation in the original test.png was due to: 1. Plotting true energy vs learned Hamiltonian 2. Using non-symplectic RK45 integrator 3. Training on noisy data Use --use-leapfrog flag to see improved conservation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors the Hamiltonian Neural Network implementation with efficiency improvements, better code organization, and comprehensive tests.
Changes
Code Organization
mlp.py: Configurable MLP backbonehnn.py: HNN with documentation and helper methodstraining.py: Training utilities for both baseline and HNNintegrators.py: Numerical integrators including symplectic leapfrogNew Features
train.pynow accepts--epochs,--lr,--seed,--use-leapfrog,--outputTests
Bug Fixes
Why Energy Oscillates in Original Code
The original test.png showed oscillating energy because:
Use
--use-leapfrogfor better conservation.Test Results: 18 passed