Skip to main content

A small library for quick fitting of multiple Gaussian Mixture Models in parallel on the GPU.

Project description

gmm_gpu

Documentation

A small library for quick fitting of multiple instances of Gaussian Mixture Models in parallel on the GPU.

This may be useful if you have a large number of independent small problems and you want to fit a GMM on each one. You can create a single large 3D tensor (three dimensional matrix) with the data for all your instances (i.e. a batch) and then send the tensor to the GPU and process the whole batch in parallel. This would work best if all the instances have roughly the same number of points.

If you have a single big problem (one GMM instance with many points) that you want to fit using the GPU, maybe Pomegranate would be a better option.

Installation

$ pip install gmm-gpu

Example usage:

Import pytorch and the GMM class

>>> from gmm_gpu.gmm import GMM
>>> import torch

Generate some test data: We create a batch of 1000 instances, each with 200 random points. Half of the points are sampled from distribution centered at the origin (0, 0) and the other half from a distribution centered at (1.5, 1.5).

>>> X1 = torch.randn(1000, 100, 2)
>>> X2 = torch.randn(1000, 100, 2) + torch.tensor([1.5, 1.5])
>>> X = torch.cat([X1, X2], dim=1)

Fit the model:

>>> gmm = GMM(n_components=2, device='cuda')
>>> gmm.fit(X)

Predict the components: This will return a matrix with shape (1000, 200) where each value is the predicted component for the point.

>>> gmm.predict(X)

Benchmarking

>>> import torch
>>> from sklearn.mixture import GaussianMixture
>>> from gmm_gpu.gmm import GMM
>>> import timeit

We generate 1,000 independent problems. In each problem we have 200 random normally distributed points centered at (0, 0) and another 200 centered at (1.5, 1.5).

>>> origin = torch.randn(1000, 200, 2)
>>> shifted = torch.randn(1000, 200, 2).add(torch.tensor([1.5, 1.5]))
>>> data_torch = torch.cat([origin, shifted], dim=1)
>>> data_numpy = data_torch.numpy()

Let's first measure the execution time of scikit-learn's GaussianMixture. We iterate over the 1,000 problems and fit a model on each one. We repeat the measurement 100 times to get more stable results.

>>> timeit.timeit("[GaussianMixture(n_components=2).fit(data_numpy[i]) for i in range(1000)]", globals=globals(), number=100)
1301.5792503219564

Then let's test this library, using the CPU to fit the models:

>>> timeit.timeit("GMM(n_components=2, device='cpu').fit(data_torch)", globals=globals(), number=100)
19.02717640507035

Finally, let's try to fit the models on the GPU:

>>> data_torch = data_torch.to('cuda')
>>> timeit.timeit("GMM(n_components=2, device='cuda').fit(data_torch)", globals=globals(), number=100)
11.06293786992319
  • The benchmarking was performed using 8 cores on Intel Xeon E5-2650 v4 and the GPU test used NVIDIA Tesla P100-PCIE-12GB.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gmm_gpu-0.2.4.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gmm_gpu-0.2.4-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file gmm_gpu-0.2.4.tar.gz.

File metadata

  • Download URL: gmm_gpu-0.2.4.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.8 Linux/6.6.78

File hashes

Hashes for gmm_gpu-0.2.4.tar.gz
Algorithm Hash digest
SHA256 377c57c8d3a703edf0bb87143dbdab2ab613feaaa3fa71c2030507134d0c9416
MD5 249b6e98ccdf43a0e8c7f72b4f56c2ec
BLAKE2b-256 92201b319de2ffc7351f92e45091caa705e4d6a33c2147716a8bdb9d12f2cc7a

See more details on using hashes here.

File details

Details for the file gmm_gpu-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: gmm_gpu-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.8 Linux/6.6.78

File hashes

Hashes for gmm_gpu-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cb5fdba9f5a4e0cf2d81f6a920c9ef54ccfee9aa12514b995e7cb4b02dd43d21
MD5 e77b01766544af0c0f0c95bc0bcda965
BLAKE2b-256 e930b5fcddc55c16a6ab6dae2bc25f0655c5286ed24ebbb6377550afc1155538

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page