{"id":15662926,"url":"https://github.com/braun-steven/simple-einet","last_synced_at":"2025-04-30T14:41:13.823Z","repository":{"id":45858920,"uuid":"419697149","full_name":"braun-steven/simple-einet","owner":"braun-steven","description":"An implementation of EinsumNetworks in PyTorch.","archived":false,"fork":false,"pushed_at":"2025-01-09T08:07:03.000Z","size":2830,"stargazers_count":21,"open_issues_count":0,"forks_count":9,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T08:20:43.503Z","etag":null,"topics":["deep-learning","einsum","machine-learning","sum-product-networks"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/braun-steven.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-10-21T11:34:59.000Z","updated_at":"2025-02-09T23:23:13.000Z","dependencies_parsed_at":"2024-10-23T02:52:01.016Z","dependency_job_id":"49f6a3da-c419-4cf7-9d06-a96a97a0520f","html_url":"https://github.com/braun-steven/simple-einet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/braun-steven%2Fsimple-einet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/braun-steven%2Fsimple-einet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/braun-steven%2Fsimple-einet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/braun-steven%2Fsimple-einet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/braun-steven","download_url":"https://codeload.github.com/braun-steven/simple-einet/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251722802,"owners_count":21633018,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","einsum","machine-learning","sum-product-networks"],"created_at":"2024-10-03T13:34:56.200Z","updated_at":"2025-04-30T14:41:13.799Z","avatar_url":"https://github.com/braun-steven.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Python version](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)\n[![License](https://img.shields.io/github/license/braun-steven/simple-einet.svg)](https://github.com/braun-steven/simple-einet/blob/main/LICENSE.md)\n[![Code style: black \u0026 isort](https://img.shields.io/badge/code%20style-black%20%26%20isort-000000.svg)](https://black.readthedocs.io/en/stable/)\n\n\n# An EinsumNetworks Implementation\n\nThis repository contains code for my personal EinsumNetworks implementation. \n\n## Notebooks\n\nThe `notebooks` directory contains Jupyter notebooks that demonstrate the usage of this library.\n\n- [Training a discriminative Einet the iris dataset](./notebooks/iris_classification.ipynb)\n- [Training a generative Einet on MNIST](./notebooks/mnist.ipynb)\n- [Training an Einet on synthetic multivariate Normal data](./notebooks/multivariate_normal.ipynb)\n\n## PyTorch Lightning Training\n\nThe `main_pl.py` script offers PyTorch-Lightning based training for discriminative and generative Einets.\n\nClassification on MNIST examples:\n\n```sh\npython main_pl.py dataset=mnist batch_size=128 epochs=100 dist=normal D=5 I=32 S=32 R=8 lr=0.001 gpu=0 classification=true \n```\n\n\u003cimg src=\"./res/mnist_classification.png\" width=400px\u003e\u003cimg src=\"./res/mnist_train_val_test_acc.png\" width=400px\u003e\n\n\nGenerative training on MNIST:\n\n``` sh\npython main_pl.py dataset=mnist D=5 I=16 R=10 S=16 lr=0.1 dist=binomial epochs=10 batch_size=128\n```\n\n![MNIST Samples]( ./res/mnist_samples.png )\n\n## Installation\n\nYou can install `simple-einet` as a dependency in your project as follows:\n\n```sh\npip install git+https://github.com/braun-steven/simple-einet\n\n```\n\nIf you want to additionally install the dependencies requires to launch the provided scripts such as `main.py`, `main_pl.py` or the notebooks, run\n\n```\npip install \"git+https://github.com/braun-steven/simple-einet#egg=simple-einet[app]\"\n```\n\nIf you plan to edit the files after installation:\n```\ngit clone git@github.com:braun-steven/simple-einet.git\ncd simple-einet\npip install -e .\n```\n\n\n## Usage Example\n\nThe following is a simple usage example of how to create, optimize, and sample from an Einet.\n\n```python\nimport torch\nfrom simple_einet.layers.distributions.normal import Normal\nfrom simple_einet.einet import Einet\nfrom simple_einet.einet import EinetConfig\n\n\nif __name__ == \"__main__\":\n    torch.manual_seed(0)\n\n    # Input dimensions\n    in_features = 4\n    batchsize = 5\n\n    # Create input sample\n    x = torch.randn(batchsize, in_features)\n\n    # Construct Einet\n    cfg = EinetConfig(\n        num_features=in_features,\n        depth=2,\n        num_sums=2,\n        num_channels=1,\n        num_leaves=3,\n        num_repetitions=3,\n        num_classes=1,\n        dropout=0.0,\n        leaf_type=Normal,\n    )\n    einet = Einet(cfg)\n\n    # Compute log-likelihoods\n    lls = einet(x)\n    print(f\"lls.shape: {lls.shape}\")\n    print(f\"lls: \\n{lls}\")\n\n    # Optimize Einet parameters (weights and leaf params)\n    optim = torch.optim.Adam(einet.parameters(), lr=0.001)\n\n    for _ in range(1000):\n        optim.zero_grad()\n\n        # Forward pass: compute log-likelihoods\n        lls = einet(x)\n\n        # Backprop negative log-likelihood loss\n        nlls = -1 * lls.sum()\n        nlls.backward()\n\n        # Update weights\n        optim.step()\n\n    # Construct samples\n    samples = einet.sample(2)\n    print(f\"samples.shape: {samples.shape}\")\n    print(f\"samples: \\n{samples}\")\n```\n\n## Citing EinsumNetworks\n\nIf you use this software, please cite it as below.\n\n```bibtex\n@software{braun2021simple-einet,\nauthor = {Braun, Steven},\ntitle = {{Simple-einet: An EinsumNetworks Implementation}},\nurl = {https://github.com/braun-steven/simple-einet},\nversion = {0.0.1},\n}\n```\n\nIf you use EinsumNetworks as a model in your publications, please cite our official EinsumNetworks paper.\n\n```bibtex\n@inproceedings{pmlr-v119-peharz20a,\n  title = {Einsum Networks: Fast and Scalable Learning of Tractable Probabilistic Circuits},\n  author = {Peharz, Robert and Lang, Steven and Vergari, Antonio and Stelzner, Karl and Molina, Alejandro and Trapp, Martin and Van Den Broeck, Guy and Kersting, Kristian and Ghahramani, Zoubin},\n  booktitle = {Proceedings of the 37th International Conference on Machine Learning},\n  pages = {7563--7574},\n  year = {2020},\n  editor = {III, Hal Daumé and Singh, Aarti},\n  volume = {119},\n  series = {Proceedings of Machine Learning Research},\n  month = {13--18 Jul},\n  publisher = {PMLR},\n  pdf = {http://proceedings.mlr.press/v119/peharz20a/peharz20a.pdf},\n  url = {http://proceedings.mlr.press/v119/peharz20a.html},\n  code = {https://github.com/cambridge-mlg/EinsumNetworks},\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbraun-steven%2Fsimple-einet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbraun-steven%2Fsimple-einet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbraun-steven%2Fsimple-einet/lists"}