torch_kmeans
PyTorch implementations of KMeans, Soft-KMeans and Constrained-KMeans
torch_kmeans features implementations of the well known k-means algorithm as well as its soft and constrained variants.
All algorithms are completely implemented as PyTorch modules and can be easily incorporated in a PyTorch pipeline or model. Therefore, they support execution on GPU as well as working on (mini-)batches of data. Moreover, they also provide a scikit-learn style interface featuring
model.fit(), model.predict() and model.fit_predict()
functions.
-> view official documentation
Highlights
Fully implemented in PyTorch. (PyTorch and Numpy are the only package dependencies!)
GPU support like native PyTorch.
PyTorch script JIT compiled for most performance sensitive parts.
- Works with mini-batches of samples:
each instance can have a different number of clusters.
- Constrained Kmeans works with cluster constraints like:
a max number of samples per cluster or,
a maximum weight per cluster, where each sample has an associated weight.
SoftKMeans is a fully differentiable clustering procedure and can readily be used in a PyTorch neural network model which requires backpropagation.
Unit tested against the scikit-learn KMeans implementation.
GPU execution enables very fast computation even for large batch size or very high dimensional feature spaces (see speed comparison)
Installation
Simply install from PyPI
pip install torch-kmeans
Usage
Pytorch style usage
import torch
from torch_kmeans import KMeans
model = KMeans(n_clusters=4)
x = torch.randn((4, 20, 2)) # (BS, N, D)
result = model(x)
print(result.labels)
Scikit-learn style usage
import torch
from torch_kmeans import KMeans
model = KMeans(n_clusters=4)
x = torch.randn((4, 20, 2)) # (BS, N, D)
model = model.fit(x)
labels = model.predict(x)
print(labels)
or
import torch
from torch_kmeans import KMeans
model = KMeans(n_clusters=4)
x = torch.randn((4, 20, 2)) # (BS, N, D)
labels = model.fit_predict(x)
print(labels)
Examples
You can find more examples and usage in the detailed example notebooks.
Metadata
Release files for torch-kmeans 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torch_kmeans-0.2.0.tar.gz | 74.5 kB | Without provenance | Details for torch_kmeans-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_kmeans-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for torch_kmeans-0.2.0-py3-none-any.whl |
Total release size: 99.1 kB
Release files / torch_kmeans-0.2.0.tar.gz
| Download URL | torch_kmeans-0.2.0.tar.gz |
|---|---|
| Size | 74.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1a07614ca499147110a58135155ab27b06f1ee9b0bb32d0f74ddadf03788da29
|
|
BLAKE2b-256 checksum How to use checksums |
4053025447c9c725f2f83d194606ee8c6867a71975d7e393697956e2907521a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.12
|
Release files / torch_kmeans-0.2.0-py3-none-any.whl
| Download URL | torch_kmeans-0.2.0-py3-none-any.whl |
|---|---|
| Size | 24.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6b6140d08b2b2ff5db9b5f8501a2d37d6f90197c2aa804f94545db3f8474a2ca
|
|
BLAKE2b-256 checksum How to use checksums |
bc210748ecc11e7dfa741178a373d8814841eba8cf81fcf8f0f3dd5a56c9a91e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.12
|
Security for torch-kmeans 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.