Metadata-Version: 2.1
Name: kmeans-gpu
Version: 0.0.4
Summary: KMeans-GPU: A PyTorch Module for KMeans.
Home-page: https://github.com/densechen/kmeans-gpu
Author: densechen
Author-email: densechen@foxmail.com
License: MIT License
Download-URL: https://github.com/densechen/kmeans-gpu/archive/main.zip
Platform: UNKNOWN
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Topic :: Documentation :: Sphinx
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Requires-Dist: torch
Requires-Dist: numpy

# kmeans-gpu

kmeans-gpu with pytorch (batch version). It is faster than sklearn.cluster.KMeans.
What's more, it is a differential operation which will back-propagate gradient to previous layers.

You can easily use `KMeans` as a `nn.Module`, and embed into your network structure.

## Install

1. From Git:

```bash
git clone git@github.com:densechen/kmeans-gpu.git
cd kmeans-gpu
pip install -r requirements.txt
python setup.py install

# check installation
python -c "import kmeans_gpu; print(kmeans_gpu.__version__)"
```

2. From PyPI:

```bash
pip install kmeans-gpu

# check installation
python -c "import kmeans_gpu; print(kmeans_gpu.__version__)"
```

## Demo

```python
from kmeans_gpu import KMeans
import torch

# Config
batch_size = 128
feature_dim = 1024
pts_dim = 3
num_pts = 256
num_cluster = 15

# Create data
features = torch.randn(batch_size, feature_dim, num_pts)
# Pay attention to the different dimension order between features and points.
points = torch.randn(batch_size, num_pts, pts_dim)

# Create KMeans Module
kmeans = KMeans(
    n_clusters=num_cluster,
    max_iter=100,
    tolerance=1e-4,
    distance='euclidean',
    sub_sampling=None,
    max_neighbors=15,
)

# Forward
centroids, features = kmeans(points, features)

print(centroids.shape, features.shape)
# output: 
# >>> torch.Size([128, 15, 3]) torch.Size([128, 1024, 15])
```

