Skip to main content

TorchKDE

Python Version PyTorch Version Tests DOI

A differentiable implementation of kernel density estimation in PyTorch by Klaus-Rudolf Kladny.

$$\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)$$

Installation Instructions

The torch-kde package can be installed via pip. Run

pip install torch-kde

Now 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:

pip install -r requirements.txt

What's included?

Kernel Density Estimation

The KernelDensity class supports the same operations as the KernelDensity class in scikit-learn, but implemented in PyTorch and differentiable with respect to input data. Here is a little taste:

from torchkde import KernelDensity
import torch

multivariate_normal = torch.distributions.MultivariateNormal(torch.ones(2), torch.eye(2))
X = multivariate_normal.sample((1000,)) # create data
X.requires_grad = True # enable differentiation
kde = KernelDensity(bandwidth=1.0, kernel='gaussian') # create kde object with isotropic bandwidth matrix
_ = kde.fit(X) # fit kde to data

X_new = multivariate_normal.sample((100,)) # create new data 
logprob = kde.score_samples(X_new)

logprob.grad_fn # is not None

You may also check out demo_kde.ipynb for a simple demo on the Bart Simpson distribution.

Tophat Kernel Approximation

The 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. for reference):

$$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),$$

where $p$ is the dimensionality of $x$. Based on this kernel, we can approximate the Tophat kernel for large values of $\beta$.

We 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.

Supported Settings

The current implementation provides the following functionality:

Feature Supported Values
Kernels Gaussian, Epanechnikov, Exponential, Tophat Approximation
Tree Algorithms Standard
Bandwidths Float (Isotropic bandwidth matrix), Scott, Silverman

Got an Extension? Create a Pull Request!

In case you do not know how to do that, here are the necessary steps:

  1. Fork the repo
  2. Create your feature branch (git checkout -b cool_tree_algorithm)
  3. Run the unit tests (python -m tests.test_kde) and only proceed if the script outputs "OK".
  4. Commit your changes (git commit -am 'Add cool tree algorithm')
  5. Push to the branch (git push origin cool_tree_algorithm)
  6. Open a Pull Request

Issues?

If 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.

"In God we trust. All others must bring data."

— W. Edwards Deming

Metadata

Release files for torch-kde 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torch-kde 0.1.5
File Size Uploaded
torch_kde-0.1.5.tar.gz 11.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torch-kde 0.1.5
File Interpreter ABI Platform
torch_kde-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 22.2 kB

Release files / torch_kde-0.1.5.tar.gz

Download URL torch_kde-0.1.5.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
efe838e81e7c926de72373b09f0334fc3ae47498d17fa3f713a1be1585db91a6
BLAKE2b-256 checksum
How to use checksums
983ca5fe40df11b558f337b2ba7d5faa7a583e656ab91d21e95d55daff39a2d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release files / torch_kde-0.1.5-py3-none-any.whl

Download URL torch_kde-0.1.5-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2ae28f5a85f6ea39e23bbf9f33c12f431dd205319cb75b1b52b0a25ced13d6f3
BLAKE2b-256 checksum
How to use checksums
8ec031fabe5da782791320679c5af5f192ecb73fc2e6e7dfda9447b294af42c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page