{"id":25403873,"url":"https://github.com/rudolfwilliam/torch-kde","last_synced_at":"2025-10-31T00:30:59.113Z","repository":{"id":267524804,"uuid":"901331908","full_name":"rudolfwilliam/torch-kde","owner":"rudolfwilliam","description":"A differentiable implementation of kernel density estimation in PyTorch.","archived":false,"fork":false,"pushed_at":"2025-02-08T15:40:16.000Z","size":789,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-08T16:27:20.573Z","etag":null,"topics":["kernel-density-estimation","python","pytorch","scikit-learn"],"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/rudolfwilliam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2024-12-10T13:16:47.000Z","updated_at":"2025-02-08T15:40:19.000Z","dependencies_parsed_at":"2025-01-16T18:29:33.086Z","dependency_job_id":"63d925e0-e415-4015-8f08-157b977f4071","html_url":"https://github.com/rudolfwilliam/torch-kde","commit_stats":null,"previous_names":["rudolfwilliam/torch-kde"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudolfwilliam%2Ftorch-kde","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudolfwilliam%2Ftorch-kde/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudolfwilliam%2Ftorch-kde/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudolfwilliam%2Ftorch-kde/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rudolfwilliam","download_url":"https://codeload.github.com/rudolfwilliam/torch-kde/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239084401,"owners_count":19578773,"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":["kernel-density-estimation","python","pytorch","scikit-learn"],"created_at":"2025-02-16T03:27:19.259Z","updated_at":"2025-10-31T00:30:59.107Z","avatar_url":"https://github.com/rudolfwilliam.png","language":"Jupyter Notebook","readme":"# TorchKDE :fire:\n\n![Python Version](https://img.shields.io/badge/python-3.11.11%2B-blue.svg)\n![PyTorch Version](https://img.shields.io/badge/pytorch-2.5.1-brightgreen.svg)\n![Tests](https://github.com/rudolfwilliam/torch-kde/actions/workflows/ci.yml/badge.svg)\n[![DOI](https://zenodo.org/badge/901331908.svg)](https://doi.org/10.5281/zenodo.14674657)\n![License](https://img.shields.io/github/license/rudolfwilliam/torch-kde)\n\nA differentiable implementation of [kernel density estimation](https://en.wikipedia.org/wiki/Kernel_density_estimation) in PyTorch.\n\n$$\\hat{f}(x) = \\frac{1}{|H|^{\\frac{1}{2}} n} \\sum_{i=1}^n K \\left( H^{-\\frac{1}{2}} \\left( x - x_i \\right) \\right)$$\n\n## Installation Instructions\n\nThe torch-kde package can be installed via `pip`. Run\n\n```bash\npip install torch-kde\n```\n\nNow you are ready to go. If you would also like to run the code from the Jupyter notebooks or contribute to this package, please also install the packages in the `requirements.txt`:\n\n```bash\npip install -r requirements.txt\n```\n\n## What is included?\n\n### Kernel Density Estimation\n\nThe `KernelDensity` class supports the same operations as the [KernelDensity class in scikit-learn](https://scikit-learn.org/dev/modules/generated/sklearn.neighbors.KernelDensity.html), but implemented in PyTorch and differentiable with respect to input data. Here is a little taste:\n\n```python\nfrom torchkde import KernelDensity\nimport torch\n\nmultivariate_normal = torch.distributions.MultivariateNormal(torch.ones(2), torch.eye(2))\nX = multivariate_normal.sample((1000,)) # create data\nX.requires_grad = True # enable differentiation\nkde = KernelDensity(bandwidth=1.0, kernel='gaussian') # create kde object with isotropic bandwidth matrix\n_ = kde.fit(X) # fit kde to data\n\nX_new = multivariate_normal.sample((100,)) # create new data \nlogprob = kde.score_samples(X_new)\n\nlogprob.grad_fn # is not None\n```\n\nYou may also check out `demo_kde.ipynb` for a simple demo on the [Bart Simpson distribution](https://www.stat.cmu.edu/~larry/=sml/densityestimation.pdf), which yields the following density estimate:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"/plots/bart_simpson_kde.svg\" width=\"500\"\u003e\n\u003c/p\u003e\n\n### Tophat Kernel Approximation\n\nThe Tophat kernel is not differentiable at two points and has zero derivative everywhere else. Thus, we provide a differentiable approximation via a generalized Gaussian (see e.g. [Pascal et al.](https://arxiv.org/pdf/1302.6498) for reference):\n\n$$K^{\\text{tophat}}(x; \\beta) = \\frac{\\beta \\Gamma \\left( \\frac{p}{2} \\right) }{\\pi^{\\frac{p}{2}} \\Gamma \\left( \\frac{p}{2\\beta} \\right) 2^{\\frac{p}{2\\beta}}} \\text{exp} \\left( - \\frac{\\| x \\|_2^{2\\beta}}{2} \\right),$$\n\nwhere $p$ is the dimensionality of $x$. Based on this kernel, we can approximate the Tophat kernel for large values of $\\beta$, as shown in the following 1-dimensional example:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"/plots/tophat_approx.svg\" width=\"500\"\u003e\n\u003c/p\u003e\n\nWe note that for $\\beta = 1$, this approximation corresponds to a Gaussian kernel. Also, while the approximation becomes better for large values of $\\beta$, its gradients with respect to the input also become larger. This is a tradeoff that must be balanced when using this kernel.\n\n## Supported Settings\n\nThe current implementation provides the following functionality:\n\n\u003cdiv align=\"center\"\u003e\n\n| Feature                  | Supported Values            |\n|--------------------------|-----------------------------|\n| Kernels                  | Gaussian, Epanechnikov, Exponential, Tophat Approximation, von Mises-Fisher (data must lie on the unit sphere) |\n| Tree Algorithms          | Standard                    |\n| Bandwidths               | Float (Isotropic bandwidth matrix), Scott, Silverman |\n| Devices | CPU, GPU |\n\n\u003c/div\u003e\n\n## Would You like to Contribute? Create a Pull Request!\n\nIn case you do not know how to do that, here are the necessary steps:\n\n1. Fork the repo\n2. Create your feature branch (`git checkout -b cool_tree_algorithm`)\n3. Run the unit tests (`python -m tests.test_kde`) and only proceed if the script outputs \"OK\".\n4. Commit your changes (`git commit -am 'Add cool tree algorithm'`)\n5. Push to the branch (`git push origin cool_tree_algorithm`)\n6. Open a Pull Request\n\n## Issues?\n\nIf you discover a bug or do not understand something, please create an issue or let me know directly at *kkladny [at] tuebingen [dot] mpg [dot] de*! I am also happy to take requests for implementing specific functionalities.\n\n\n\u003cdiv align=\"center\"\u003e\n\n\u003e \"In God we trust. All others must bring data.\"\n\u003e \n\u003e — W. Edwards Deming\n\u003e \n\u003c/div\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frudolfwilliam%2Ftorch-kde","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frudolfwilliam%2Ftorch-kde","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frudolfwilliam%2Ftorch-kde/lists"}