{ "info": { "author": "Charles Martin", "author_email": "charlepm@ifi.uio.no", "bugtrack_url": null, "classifiers": [ "Development Status :: 2 - Pre-Alpha", "Intended Audience :: Science/Research", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3", "Topic :: Scientific/Engineering :: Artificial Intelligence" ], "description": "# Keras Mixture Density Network Layer\n\n[![Build Status](https://travis-ci.com/cpmpercussion/keras-mdn-layer.svg?branch=master)](https://travis-ci.com/cpmpercussion/keras-mdn-layer)\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).\u00a0You 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 dimensiona and number of mixtures.\n- `sample_from_output(params, output_dim, num_mixtures, temp=1.0)`: This functions samples from the mixture distribution output by the model.\n\n## Installation \n\nThis project requires Python 3.6+. You can easily 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 `mdn` module in Python: `import mdn`\n\nAlternatively, you can clone or download this repository and then install via `python setup.py install`, or copy the `mdn` folder into your own project.\n\n## Examples\n\nSome examples are provided in the notebooks directory.\n\nThere's scripts for fitting multivalued functions, a standard MDN toy problem:\n\n\"Keras\n\nThere's also a script for generating fake kanji characters:\n\n\"kanji\n\nAnd finally, for learning how to generate musical touch-screen performances with a temporal component:\n\n\"Robojam\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 import keras\n import 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(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## 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\u2019s thesis. Universitat Polit\u00e8cnica 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 \u201918, 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\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/cpmpercussion/keras-mdn-layer", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "keras-mdn-layer", "package_url": "https://pypi.org/project/keras-mdn-layer/", "platform": "", "project_url": "https://pypi.org/project/keras-mdn-layer/", "project_urls": { "Homepage": "https://github.com/cpmpercussion/keras-mdn-layer" }, "release_url": "https://pypi.org/project/keras-mdn-layer/0.2.2/", "requires_dist": null, "requires_python": "", "summary": "An MDN Layer for Keras using TensorFlow Probability.", "version": "0.2.2" }, "last_serial": 5728007, "releases": { "0.2.1": [ { "comment_text": "", "digests": { "md5": "8961fc63acf675f2fb49f130d94b77aa", "sha256": "67c62f8737102b868d092d6203d618986333b8aaef0c044e0d317d6ca07fbc54" }, "downloads": -1, "filename": "keras_mdn_layer-0.2.1-py3-none-any.whl", "has_sig": false, "md5_digest": "8961fc63acf675f2fb49f130d94b77aa", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7587, "upload_time": "2019-08-25T20:53:20", "url": "https://files.pythonhosted.org/packages/53/19/9c15fab77bf1232bddbeca79328a0264204e30a03b828f7edf73b7c9aadf/keras_mdn_layer-0.2.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "dd6757048d2300950628564cfa8b323e", "sha256": "83fddd7f01a8d79a5acffabf90c1fd118e543f90850bdfaa17dd6fd2825f5dfc" }, "downloads": -1, "filename": "keras-mdn-layer-0.2.1.tar.gz", "has_sig": false, "md5_digest": "dd6757048d2300950628564cfa8b323e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6526, "upload_time": "2019-08-25T20:53:22", "url": "https://files.pythonhosted.org/packages/92/9a/7a7945223cb92948c553763b67423019f72759196dbcfbbc594b62b011ae/keras-mdn-layer-0.2.1.tar.gz" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "0488794042e1a1efad2542e3b5a07dea", "sha256": "669978e1ecb85b5afbb97db9f985c3876ecef59f9c646758242b04776464f3ce" }, "downloads": -1, "filename": "keras_mdn_layer-0.2.2-py3-none-any.whl", "has_sig": false, "md5_digest": "0488794042e1a1efad2542e3b5a07dea", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7619, "upload_time": "2019-08-25T21:00:26", "url": "https://files.pythonhosted.org/packages/6e/21/82a1402ff0a6cbd85fd4bde839737d7651e5a6cf6f2ada58fd2d7399caf1/keras_mdn_layer-0.2.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ba0bab85eafc12d6c59983fc540d2d83", "sha256": "b2fd84ca4d38b3ac7a6e0728c04bc29472fec1e9acfe24ce4fbeb5ec11f9d8a0" }, "downloads": -1, "filename": "keras-mdn-layer-0.2.2.tar.gz", "has_sig": false, "md5_digest": "ba0bab85eafc12d6c59983fc540d2d83", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6552, "upload_time": "2019-08-25T21:00:28", "url": "https://files.pythonhosted.org/packages/69/b0/a3910b414ae09d50ec4357c8faef674d1e6eb308072d1085f517ebd968fa/keras-mdn-layer-0.2.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "0488794042e1a1efad2542e3b5a07dea", "sha256": "669978e1ecb85b5afbb97db9f985c3876ecef59f9c646758242b04776464f3ce" }, "downloads": -1, "filename": "keras_mdn_layer-0.2.2-py3-none-any.whl", "has_sig": false, "md5_digest": "0488794042e1a1efad2542e3b5a07dea", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7619, "upload_time": "2019-08-25T21:00:26", "url": "https://files.pythonhosted.org/packages/6e/21/82a1402ff0a6cbd85fd4bde839737d7651e5a6cf6f2ada58fd2d7399caf1/keras_mdn_layer-0.2.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ba0bab85eafc12d6c59983fc540d2d83", "sha256": "b2fd84ca4d38b3ac7a6e0728c04bc29472fec1e9acfe24ce4fbeb5ec11f9d8a0" }, "downloads": -1, "filename": "keras-mdn-layer-0.2.2.tar.gz", "has_sig": false, "md5_digest": "ba0bab85eafc12d6c59983fc540d2d83", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6552, "upload_time": "2019-08-25T21:00:28", "url": "https://files.pythonhosted.org/packages/69/b0/a3910b414ae09d50ec4357c8faef674d1e6eb308072d1085f517ebd968fa/keras-mdn-layer-0.2.2.tar.gz" } ] }