项目作者: subhadarship

项目描述 :
kmeans using PyTorch
高级语言: Jupyter Notebook
项目地址: git://github.com/subhadarship/kmeans_pytorch.git
创建时间: 2019-12-25T07:43:23Z
项目社区:https://github.com/subhadarship/kmeans_pytorch

开源协议:MIT License

下载


K Means using PyTorch

PyTorch implementation of kmeans for utilizing GPU

Alt Text

Getting Started

  1. import torch
  2. import numpy as np
  3. from kmeans_pytorch import kmeans
  4. # data
  5. data_size, dims, num_clusters = 1000, 2, 3
  6. x = np.random.randn(data_size, dims) / 6
  7. x = torch.from_numpy(x)
  8. # kmeans
  9. cluster_ids_x, cluster_centers = kmeans(
  10. X=x, num_clusters=num_clusters, distance='euclidean', device=torch.device('cuda:0')
  11. )

see example.ipynb for a more elaborate example

Requirements

  • PyTorch version >= 1.0.0
  • Python version >= 3.6

Installation

install with pip:

  1. pip install kmeans-pytorch

Installing from source

To install from source and develop locally:

  1. git clone https://github.com/subhadarship/kmeans_pytorch
  2. cd kmeans_pytorch
  3. pip install --editable .

CPU vs GPU

see cpu_vs_gpu.ipynb for a comparison between CPU and GPU

Notes

  • useful when clustering large number of samples
  • utilizes GPU for faster matrix computations
  • support euclidean and cosine distances (for now)

Credits

  • This implementation closely follows the style of this
  • Documentation is done using the awesome theme jekyllbook

License

MIT