TorchEBM Examples¶
This section contains practical examples that demonstrate how to use TorchEBM for energy-based modeling. Each example is fully tested and focuses on a specific use case or feature.
- 
Energy Functions 
 Explore and visualize various energy functions and their properties. 
- 
Langevin Dynamics 
 Sample from various distributions using Langevin dynamics. 
- 
Hamiltonian Monte Carlo 
 Learn to use Hamiltonian Monte Carlo for efficient sampling. 
- 
Visualization Tools 
 Advanced visualization tools for energy landscapes and sampling results. 
Example Structure¶
Example Format
Each example follows a consistent structure to help you understand and apply the concepts:
- Overview: Brief explanation of the example and its purpose
- Code: Complete, runnable code for the example
- Explanation: Detailed explanation of key concepts and code sections
- Extensions: Suggestions for extending or modifying the example
Running the Examples¶
All examples can be run using the examples main.py script:
# Clone the repository
git clone https://github.com/soran-ghaderi/torchebm.git
cd torchebm
# Set up your environment
pip install -e .
# List all available examples
python examples/main.py --list
# Run a specific example
python examples/main.py samplers/langevin/visualization_trajectory
Prerequisites¶
To run these examples, you'll need:
- Python 3.7+
- PyTorch 1.9+
- NumPy
- Matplotlib
If you haven't installed TorchEBM yet, see the Installation guide.
GPU Acceleration¶
Most examples support GPU acceleration and will automatically use CUDA if available:
Key Example Files¶
You'll find examples organized into the following categories:
| Category | Description | Key Files | 
|---|---|---|
| core/energy_functions/ | Energy function visualization and properties | landscape_2d.py,multimodal.py,parametric.py | 
| samplers/langevin/ | Langevin dynamics sampling examples | gaussian_sampling.py,multimodal_sampling.py,visualization_trajectory.py | 
| samplers/hmc/ | Hamiltonian Monte Carlo examples | gaussian_sampling.py,advanced.py,mass_matrix.py | 
| visualization/ | Advanced visualization tools | basic/contour_plots.py,advanced/trajectory_animation.py,advanced/parallel_chains.py | 
Additional Resources¶
For more in-depth information about the concepts demonstrated in these examples, see:
What's Next?¶
After exploring these examples, you might want to:
- Check out the API Reference for detailed documentation
- Read the Developer Guide to learn about contributing
- Look at the roadmap for upcoming features