{"id":26546678,"url":"https://github.com/cpmpercussion/keras-mdn-layer","last_synced_at":"2026-04-01T19:07:09.777Z","repository":{"id":37602601,"uuid":"137585470","full_name":"cpmpercussion/keras-mdn-layer","owner":"cpmpercussion","description":"An MDN Layer for Keras using TensorFlow's distributions module","archived":false,"fork":false,"pushed_at":"2026-03-17T04:03:09.000Z","size":6116,"stargazers_count":170,"open_issues_count":10,"forks_count":43,"subscribers_count":6,"default_branch":"master","last_synced_at":"2026-03-17T10:03:39.646Z","etag":null,"topics":["keras","mdn","mdn-rnn","mixture-density-network","neural-network","tensorflow"],"latest_commit_sha":null,"homepage":null,"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/cpmpercussion.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":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2018-06-16T14:13:36.000Z","updated_at":"2026-03-17T04:03:12.000Z","dependencies_parsed_at":"2024-06-07T08:27:22.691Z","dependency_job_id":"3042b067-986f-44c3-bb56-f1141456ed44","html_url":"https://github.com/cpmpercussion/keras-mdn-layer","commit_stats":{"total_commits":97,"total_committers":3,"mean_commits":"32.333333333333336","dds":"0.020618556701030966","last_synced_commit":"17777671e0c28fc200573a3ea5ff629c267b25d2"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/cpmpercussion/keras-mdn-layer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cpmpercussion%2Fkeras-mdn-layer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cpmpercussion%2Fkeras-mdn-layer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cpmpercussion%2Fkeras-mdn-layer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cpmpercussion%2Fkeras-mdn-layer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cpmpercussion","download_url":"https://codeload.github.com/cpmpercussion/keras-mdn-layer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cpmpercussion%2Fkeras-mdn-layer/sbom","scorecard":{"id":306679,"data":{"date":"2025-08-11","repo":{"name":"github.com/cpmpercussion/keras-mdn-layer","commit":"bf102dc404d2e04daa975776c195b23c15b27653"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.4,"checks":[{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Token-Permissions","score":10,"reason":"GitHub workflow tokens follow principle of least privilege","details":["Info: topLevel 'contents' permission set to 'read': .github/workflows/python-app.yml:10","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Code-Review","score":0,"reason":"Found 0/17 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/python-app.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/cpmpercussion/keras-mdn-layer/python-app.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/python-app.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/cpmpercussion/keras-mdn-layer/python-app.yml/master?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/python-app.yml:45: update your workflow using https://app.stepsecurity.io/secureworkflow/cpmpercussion/keras-mdn-layer/python-app.yml/master?enable=pin","Info:   0 out of   2 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   1 third-party GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE.md:0","Info: FSF or OSI recognized license: MIT License: LICENSE.md:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 15 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"18 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-vqfr-h8mv-ghfj","Warn: Project is vulnerable to: GHSA-cpwx-vrp4-4pq7","Warn: Project is vulnerable to: GHSA-gmj6-6f8f-6699","Warn: Project is vulnerable to: GHSA-q2x7-8rv6-6q7h","Warn: Project is vulnerable to: GHSA-33p9-3p43-82vq","Warn: Project is vulnerable to: GHSA-9q39-rmj3-p4r2","Warn: Project is vulnerable to: GHSA-48g7-3x6r-xfhp","Warn: Project is vulnerable to: GHSA-c9rc-mg46-23w3","Warn: Project is vulnerable to: GHSA-cjgq-5qmw-rcj6","Warn: Project is vulnerable to: GHSA-8qvm-5x2c-j2w7","Warn: Project is vulnerable to: GHSA-9hjg-9r4m-mvj7","Warn: Project is vulnerable to: PYSEC-2025-49 / GHSA-5rjg-fvgr-3xxf","Warn: Project is vulnerable to: GHSA-7cx3-6m66-7c5m","Warn: Project is vulnerable to: GHSA-8w49-h785-mj3c","Warn: Project is vulnerable to: GHSA-48p4-8xcf-vxj5","Warn: Project is vulnerable to: GHSA-pq67-6m6q-mj2v","Warn: Project is vulnerable to: GHSA-f9vj-2wh5-fj8j","Warn: Project is vulnerable to: GHSA-q34m-jh98-gwm2"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-17T22:13:36.306Z","repository_id":37602601,"created_at":"2025-08-17T22:13:36.306Z","updated_at":"2025-08-17T22:13:36.306Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31291064,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-01T13:12:26.723Z","status":"ssl_error","status_checked_at":"2026-04-01T13:12:25.102Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["keras","mdn","mdn-rnn","mixture-density-network","neural-network","tensorflow"],"created_at":"2025-03-22T05:19:50.239Z","updated_at":"2026-04-01T19:07:09.768Z","avatar_url":"https://github.com/cpmpercussion.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Keras Mixture Density Network Layer\n\n[![Coverage Status](https://coveralls.io/repos/github/cpmpercussion/keras-mdn-layer/badge.svg?branch=master)](https://coveralls.io/github/cpmpercussion/keras-mdn-layer?branch=master)\n[![Build and test keras-mdn-layer](https://github.com/cpmpercussion/keras-mdn-layer/actions/workflows/python-app.yml/badge.svg)](https://github.com/cpmpercussion/keras-mdn-layer/actions/workflows/python-app.yml)\n![MIT License](https://img.shields.io/github/license/cpmpercussion/keras-mdn-layer.svg?style=flat)\n[![DOI](https://zenodo.org/badge/137585470.svg)](https://zenodo.org/badge/latestdoi/137585470)\n[![PyPI version](https://badge.fury.io/py/keras-mdn-layer.svg)](https://badge.fury.io/py/keras-mdn-layer)\n\nA mixture density network (MDN) Layer for Keras using TensorFlow's distributions module. This makes it a bit more simple to experiment with neural networks that predict multiple real-valued variables that can take on multiple equally likely values.\n\nThis layer can help build MDN-RNNs similar to those used in [RoboJam](https://github.com/cpmpercussion/robojam), [Sketch-RNN](https://experiments.withgoogle.com/sketch-rnn-demo), [handwriting generation](https://distill.pub/2016/handwriting/), and maybe even [world models](https://worldmodels.github.io). You can do a lot of cool stuff with MDNs!\n\nOne benefit of this implementation is that you can predict any number of real-values. TensorFlow's `Mixture`, `Categorical`, and `MultivariateNormalDiag` distribution functions are used to generate the loss function (the probability density function of a mixture of multivariate normal distributions with a diagonal covariance matrix). In previous work, the loss function has often been specified by hand which is fine for 1D or 2D prediction, but becomes a bit more annoying after that.\n\nTwo important functions are provided for training and prediction:\n\n- `get_mixture_loss_func(output_dim, num_mixtures)`: This function generates a loss function with the correct output dimensions and number of mixtures.\n- `sample_from_output(params, output_dim, num_mixtures, temp=1.0, sigma_temp=1.0)`: This function samples from the mixture distribution output by the model.\n\n## Installation\n\nThis project requires Python 3.11+, TensorFlow 2.16+, and TensorFlow Probability 0.24+. You can install this package from [PyPI](https://pypi.org/project/keras-mdn-layer/) via `pip` like so:\n\n    python3 -m pip install keras-mdn-layer\n\nAnd finally, import the module in Python: `import keras_mdn_layer as mdn`\n\nAlternatively, you can clone or download this repository and then install via `poetry install`.\n\n## Tested Configurations\n\nThis library is tested against the following platform, Python, and TensorFlow combinations:\n\n| TensorFlow | TF Probability | tf-keras | Python       | Platforms            |\n|------------|----------------|----------|--------------|----------------------|\n| 2.15.1     | 0.23.0         | 2.15.1   | 3.11         | Ubuntu               |\n| 2.16.2     | 0.24.0         | 2.16.0   | 3.11, 3.12   | Ubuntu, macOS        |\n| 2.18.1     | 0.25.0         | —        | 3.11, 3.12   | Ubuntu, macOS        |\n| 2.20.0     | 0.25.0         | —        | 3.11–3.13    | Ubuntu, macOS, Windows |\n\nOther combinations may work but are not regularly tested in CI.\n\n## Build\n\nThis project builds using `poetry`. To build a wheel use `poetry build`.\n\n## Examples\n\nSome examples are provided in the notebooks directory.\n\nTo run these using `poetry`, run `poetry install` and then open jupyter `poetry run jupyter lab`.\n\nThere's scripts for fitting multivalued functions, a standard MDN toy problem:\n\n\u003cimg src=\"https://preview.ibb.co/mZzkpd/Keras_MDN_Demo.jpg\" alt=\"Keras MDN Demo\" border=\"0\"\u003e\n\nThere's also a script for generating fake kanji characters:\n\n\u003cimg src=\"https://i.ibb.co/yFvtgkL/kanji-mdn-examples.png\" alt=\"kanji test 1\" border=\"0\" width=\"600\"/\u003e\n\nAnd finally, for learning how to generate musical touch-screen performances with a temporal component:\n\n\u003cimg src=\"https://i.ibb.co/WpzSCV8/robojam-examples.png\" alt=\"Robojam Model Examples\" border=\"0\"\u003e\n\n## How to use\n\nThe MDN layer should be the last in your network and you should use `get_mixture_loss_func` to generate a loss function. Here's an example of a simple network with one Dense layer followed by the MDN.\n\n    from tensorflow import keras\n    import keras_mdn_layer as mdn\n\n    N_HIDDEN = 15  # number of hidden units in the Dense layer\n    N_MIXES = 10  # number of mixture components\n    OUTPUT_DIMS = 2  # number of real-values predicted by each mixture component\n\n    model = keras.Sequential()\n    model.add(keras.layers.Dense(N_HIDDEN, batch_input_shape=(None, 1), activation='relu'))\n    model.add(mdn.MDN(OUTPUT_DIMS, N_MIXES))\n    model.compile(loss=mdn.get_mixture_loss_func(OUTPUT_DIMS,N_MIXES), optimizer=keras.optimizers.Adam())\n    model.summary()\n\nFit as normal:\n\n    history = model.fit(x=x_train, y=y_train)\n\nThe predictions from the network are parameters of the mixture models, so you have to apply the `sample_from_output` function to generate samples.\n\n    y_test = model.predict(x_test)\n    y_samples = np.apply_along_axis(mdn.sample_from_output, 1, y_test, OUTPUT_DIMS, N_MIXES, temp=1.0)\n\nSee the notebooks directory for examples in jupyter notebooks!\n\n### Load/Save Model\n\nSaving models is straight forward:\n\n    model.save('test_save.keras')\n\nBut loading requires `custom_objects` to be filled with the MDN layer, and a loss function with the appropriate parameters:\n\n    m_2 = keras.models.load_model('test_save.keras', custom_objects={'MDN': mdn.MDN, 'mdn_loss_func': mdn.get_mixture_loss_func(1, N_MIXES)})\n\n## Acknowledgements\n\n- Hat tip to [Omimo's Keras MDN layer](https://github.com/omimo/Keras-MDN) for a starting point for this code.\n- Super hat tip to [hardmaru's MDN explanation, projects, and good ideas for sampling functions](http://blog.otoro.net/2015/11/24/mixture-density-networks-with-tensorflow/) etc.\n- Many good ideas from [Axel Brando's Master's Thesis](https://github.com/axelbrando/Mixture-Density-Networks-for-distribution-and-uncertainty-estimation)\n- Mixture Density Networks in Edward [tutorial](http://edwardlib.org/tutorials/mixture-density-network).\n\n## References\n\n1. Christopher M. Bishop. 1994. Mixture Density Networks. [Technical Report NCRG/94/004](http://publications.aston.ac.uk/373/). Neural Computing Research Group, Aston University. http://publications.aston.ac.uk/373/\n2. Axel Brando. 2017. Mixture Density Networks (MDN) for distribution and uncertainty estimation. Master’s thesis. Universitat Politècnica de Catalunya.\n3. A. Graves. 2013. Generating Sequences With Recurrent Neural Networks. ArXiv e-prints (Aug. 2013). https://arxiv.org/abs/1308.0850\n4. David Ha and Douglas Eck. 2017. A Neural Representation of Sketch Drawings. ArXiv e-prints (April 2017). https://arxiv.org/abs/1704.03477\n5. Charles P. Martin and Jim Torresen. 2018. RoboJam: A Musical Mixture Density Network for Collaborative Touchscreen Interaction. In Evolutionary and Biologically Inspired Music, Sound, Art and Design: EvoMUSART ’18, A. Liapis et al. (Ed.). Lecture Notes in Computer Science, Vol. 10783. Springer International Publishing. DOI:[10.1007/9778-3-319-77583-8_11](http://dx.doi.org/10.1007/9778-3-319-77583-8_11)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcpmpercussion%2Fkeras-mdn-layer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcpmpercussion%2Fkeras-mdn-layer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcpmpercussion%2Fkeras-mdn-layer/lists"}