Moment Neural Network
Moment Neural Network (MNN) is a PyTorch-based framework for building, training, and simulating neural networks that propagate second-order statistics. Instead of tracking only mean neural activity, an MNN tracks both mean activity and covariance, making it suitable for studying correlated neural variability in spiking neural systems.
The project is motivated by stochastic neural computing for spiking neural networks (SNNs). Once trained, MNN parameters can be used to recover a corresponding SNN without an additional fine-tuning phase. The trained model is intended to capture realistic firing statistics, including firing-rate distributions, Fano factors, and weak pairwise correlations.
For full details, see the associated publication: https://doi.org/10.1093/pnasnexus/pgaf284
What This Project Provides
- Differentiable MNN layers that transform both mean and covariance.
- Moment activation functions derived from leaky integrate-and-fire neuron statistics.
- MLP model wrappers for MNN, SNN-like, ANN, and mean-only baselines.
- YAML-driven training utilities compatible with standard PyTorch optimizers and TorchVision datasets.
- Utilities to recover and simulate SNNs from trained MNN checkpoints.
- Tutorials and publication-specific experiment scripts.
Installation
Install the project through pip:
pip install moment-neural-network
Alternatively, clone the repository and install it from the repository root:
git clone git@github.com:BrainsoupFactory/moment-neural-network.git
cd moment-neural-network
python -m pip install -e .
The package metadata requires Python 3.8 or newer and installs:
torchtorchvisionnumpyscipyPyYAMLtqdm
Optional extras:
python -m pip install -e ".[analysis]"
python -m pip install -e ".[loihi]"
The original development environment used PyTorch 1.12.1, TorchVision 0.13.1, SciPy 1.7.3, PyYAML 6.0, and NumPy 1.22.3. Newer versions may work, but use those versions as a starting point for reproducibility-sensitive experiments.
CUDA is optional but recommended for larger MNNs and SNN simulations with many trials.
Repository Structure
mnn/mnn_core: core MNN math and PyTorch autograd bindings.mnn/mnn_core/nn: layers, activations, losses, batch normalization, pooling, and functional utilities.mnn/models: model wrappers, includingMnnMlp,SnnMlp,AnnMlp, and mean-only variants.mnn/snn: MNN-to-SNN conversion and SNN simulation utilities.mnn/snn/base: LIF neurons, current generators, monitors, and probes.mnn/utils/training_tools: config loading, dataloaders, training, checkpointing, logging, and resume helpers.mnn/utils/dataloaders: dataset loaders for MNIST and related examples.mnn/analysis: analysis and visualization helpers.docs: Sphinx documentation and tutorial notebooks.examples/mnist: runnable MNIST training and SNN validation example.publications: scripts and configs used for paper-specific experiments.
Quick Start: MNIST
Create output and data directories:
mkdir -p checkpoint data
Run the MNIST example:
python examples/mnist/mnist.py --config=examples/mnist/mnist_config.yaml
Important: the current examples/mnist/mnist.py defaults to running SNN
simulation in main(), while train_mnist(config) is commented out.
For a first training run, edit main() to train:
def main():
config = utils.training_tools.deploy_config()
train_mnist(config)
# mnn2snn_simulation(config)
After training, switch back to SNN validation:
def main():
config = utils.training_tools.deploy_config()
# train_mnist(config)
mnn2snn_simulation(config)
By default, outputs are written under:
checkpoint/mnist/
Typical files include:
mnn_net_config.yaml: saved copy of the effective config.mnn_net_log.txt: training and validation log.mnn_net.pth: latest checkpoint.mnn_net_best_model.pth: best validation checkpoint.mnn_net_snn_validate_result/: SNN simulation outputs.
Configuration Workflow
The YAML config controls model construction, optimization, data transforms, input encoding, and output paths.
Important sections:
MODEL: model family and architecture. The MNIST example usesarch: mnn_mlpandmlp_type: mnn_mlp.CRITERION: loss function. The example usesCrossEntropyOnMean.OPTIMIZER: PyTorch optimizer name and arguments. The example usesAdamW.DATAAUG_TRAINandDATAAUG_VAL: TorchVision transform pipelines.input_prepare: input-to-moment encoding.flatten_poissonflattens images and creates diagonal covariance from Poisson-like rates.scale_factor: multiplier applied to input firing-rate-like values.background_noise: optional diagonal covariance offset.dump_path,dir, andsave_name: output checkpoint location and file prefix.
Command-line arguments are parsed by
mnn/utils/training_tools/general_prepare.py. Example:
python examples/mnist/mnist.py \
--config=examples/mnist/mnist_config.yaml \
--bs=128 \
--epochs=10 \
--cpu
Values loaded from the YAML config can overwrite command-line defaults when the same key is present in the config.
Using MNN Layers Directly
For custom research code, you can bypass the wrapper pipeline and use MNN layers directly:
import torch
from mnn.mnn_core.nn.linear import LinearDuo
from mnn.mnn_core.nn.custom_batch_norm import CustomBatchNorm1D
from mnn.mnn_core.nn.activation import OriginMnnActivation
from mnn.mnn_core.nn.criterion import CrossEntropyOnMean
class SimpleMNN(torch.nn.Module):
def __init__(self, input_size=784, hidden_size=100, output_size=10):
super().__init__()
self.linear = LinearDuo(input_size, hidden_size)
self.bn = CustomBatchNorm1D(hidden_size)
self.act = OriginMnnActivation()
self.readout = LinearDuo(hidden_size, output_size, bias=True)
def forward(self, inputs):
u, cov = inputs
u, cov = self.linear(u, cov)
u, cov = self.bn(u, cov)
u, cov = self.act(u, cov)
return self.readout(u, cov)
def encode_poisson_images(images, scale=1.0):
mean = torch.flatten(images, start_dim=1) * scale
cov = torch.diag_embed(torch.abs(mean))
return mean, cov
model = SimpleMNN()
criterion = CrossEntropyOnMean()
See docs/tutorials/tutorial_training_mnn_vanilla.ipynb for a more
complete direct-use example.
SNN Reconstruction and Simulation
After a model has been trained and checkpointed, use
snn.functional.MnnSnnValidate or a task-specific subclass such as the
MNIST example's MnistSnnValidate.
Typical simulation parameters:
running_time: simulated duration in ms.dt: simulation time step.num_trials: number of SNN trials simulated in parallel.input_type:poissonorgaussian.resume_best: whether to load the best or latest checkpoint.pregenerate: whether to pregenerate stochastic inputs.
Simulation outputs are saved as:
.snnval: summary data such as MNN output, SNN output, spike counts, target, prediction, run time, and input type..spt: recorded spike trains as sparse tensors.
Tutorials
The Sphinx documentation and tutorials are in docs/.
Recommended reading order:
docs/tutorials/tutorial_moment_activation.ipynbdocs/tutorials/tutorial_training_mnn.ipynbdocs/tutorials/tutorial_training_mnn_vanilla.ipynbdocs/tutorials/tutorial_EI_network.ipynb
Publication Code
The publications folder contains experiment-specific code:
Qi2024pre: code for Moment neural network and an efficient numerical method for modeling irregular spiking activity.Zhu2024plos: training code for Learning to integrate parts for whole through correlated neural variability.Zhu2024neco: training code for Toward a Free-Response Paradigm of Decision Making in Spiking Neural Networks.Ma2023neco: additional publication material.
The reusable project API lives under mnn; publication folders are most
useful for reproducing paper-specific experiments.
Current Limitations
- The most mature model path is MLP-based MNN training.
- Full covariance scales quadratically with layer width, so memory use can become large.
- The convolution implementation is present but less developed than the MLP stack.
- Some publication scripts are environment-specific and may need path, dataset, or device edits before running.
- SNN simulation is slower than an MNN forward pass because it advances a stochastic process over many time steps.
Lead Authors
- Zhichao Zhu - Chief Architect - Zhichao Zhu
- Yang Qi - Lead Algorithm Design - Yang Qi
License
This project is licensed under the Apache License 2.0. See LICENSE for details.
Metadata
Release files for moment-neural-network 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| moment_neural_network-0.2.1.tar.gz | 106.3 kB | Without provenance | Details for moment_neural_network-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| moment_neural_network-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for moment_neural_network-0.2.1-py3-none-any.whl |
Total release size: 229.5 kB
Release files / moment_neural_network-0.2.1.tar.gz
| Download URL | moment_neural_network-0.2.1.tar.gz |
|---|---|
| Size | 106.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
41e916d0941fb60a71551b0a505ac757af6f24f2dd332d1ab597fbdd52d0dc7e
|
|
BLAKE2b-256 checksum How to use checksums |
b184e23c92fc1c7c1bce456293bca73135ceacbdf379a062266d2cce189080fb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.9
|
Release files / moment_neural_network-0.2.1-py3-none-any.whl
| Download URL | moment_neural_network-0.2.1-py3-none-any.whl |
|---|---|
| Size | 123.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
76356d7fc2476813db608fa89b2943446e03c2d26307e4921afc539ced0b3a89
|
|
BLAKE2b-256 checksum How to use checksums |
5b0c09a02d4326d62a20a294560aeb9905a6b7598dffb077b74d0123ce73d487
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.9
|
Security for moment-neural-network 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.