Metadata-Version: 2.4
Name: sesamo
Version: 1.0.0
Summary: SESaMo provides an extension to Normalizing Flows that enforces symmetries to the output distribution.
Author-email: Janik Kreit <jkreit@uni-bonn.de>
License: MIT License
        
        Copyright (c) 2025 Janik Kreit
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/sesamo-project/SESaMo
Requires-Python: <3.13,>=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.2
Requires-Dist: numpy<2,>=1.24
Requires-Dist: tqdm
Requires-Dist: matplotlib
Requires-Dist: tensorboard
Requires-Dist: hydra-core
Requires-Dist: hydra-submitit-launcher
Requires-Dist: nflows
Requires-Dist: setproctitle
Dynamic: license-file

[![Paper](https://img.shields.io/badge/Paper-arXiv-red)](https://arxiv.org/abs/2505.19619)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

# SESaMo: Symmetry-Enforcing Stochastic Modulation for Normalizing Flows

## Quick start

Install the package with pip:
```bash
pip install sesamo
```

Here is a quick example of how to use SESaMo to build a normalizing flow with stochastic modulation:
```python
import torch
from sesamo import Sesamo
from sesamo.models import GaussianPrior, RealNVP, Z2Modulation, Z2Regularization
from sesamo.loss import StochmodLoss

# Initialize SESaMo
sesamo = Sesamo(
    prior=GaussianPrior(
        var=1,
        lat_shape=[1,2]
    ),
    flow=RealNVP(
        lat_shape=[1,2],
        num_coupling_layers=10,
        num_hidden_layers=2,
        num_hidden_features=40
    ),
    stochastic_modulation=Z2Modulation(),
    regularization=Z2Regularization(),
).to("cuda")

action = # define action for the target distribution p(x) = exp(-action(x)) / Z
loss_fn = StochmodLoss()
optimizer = torch.optim.Adam(sesamo.parameters(), lr=5e-4)

# Training loop
for _ in range(10_000):
    # reset gradients
    optimizer.zero_grad()

    # sample from sesamo
    samples, log_prob, log_prob_stochmod, penalty = sesamo.sample_for_training(8_000)
    
    # compute action and loss
    action_samples = action(samples)
    loss = loss_fn(action_samples, log_prob, log_prob_stochmod, penalty).mean()
    
    # backpropagate and update flow parameters
    loss.backward()
    optimizer.step()
```

### Examples

For more examples see the ```SESaMo/examples``` folder, which contains Jupyter notebooks for the Hubbard model and the Gaussian mixture model.


## Run experiments

To run the experiments from the paper, follow the instructions below.

Clone the repository and move into the directory:
```
git clone https://github.com/janikkreit/SESaMo.git
cd SESaMo
```

Create a python virtual environment and install the package:
```
python -m venv .venv
source .venv/bin/activate
pip install -e .
```

Run experiments with
```
cd experiments
python train.py -cp configs/<experiment> -cn <model>
```

Available ```<experiment>```s are:
```
hubbard2x1
hubbard18x100
gaussian-mixture
broken-gaussian-mixture
complex-phi4
broken-complex-phi4
broken-scalar-phi4
```

Available ```<model>```s are:
```
realnvp
vmonf
canonicalization
sesamo
```

The checkpoint, tensorboard, config and stats files are stored in the ```SESaMo/scripts/runs``` folder.
After training is completed or interupted the distribution is plotted and saved as ```SESaMo/scripts/runs/.../samples.png```



## Citation
If you use SESaMo in your research, please consider citing our paper:
```
@article{kreit2025sesamo,
    title={SESaMo: Symmetry-Enforcing Stochastic Modulation for Normalizing Flows}, 
    author={Janik Kreit and Dominic Schuh and Kim A. Nicoli and Lena Funcke},
    year={2025},
    eprint={2505.19619},
    archivePrefix={arXiv},
    primaryClass={cs.LG},
    url={https://arxiv.org/abs/2505.19619}, 
}
