Skip to main content

Self Organising Maps in pytorch

Project description

KSOM - Simple, but relatively efficient, pytorch-based self organising map

This is a simple implementation of self-organising map training in python, using pytorch for efficiency. This enables to create, train and apply square maps of potentially high dimensions on CPU or GPU.

To install, use

pip install ksom

An example is available in test/test-img.py for a simple use case creating a square color map of an image. Having loaded the data in a tensor x, the code to initialise and train the SOM looks like this:

from ksom import SOM, cosine_distance, nb_gaussian
...
smodel = SOM(6, 6, 3, # size of the map and dimension of units
             sample_init=samples, # initialised with samples
             dist=cosine_distance, # using cosine distance for BMU
             alpha_init=0.01, # learning rate
             alpha_drate=1e-7, # decay of learning rate
             neighborhood_fct=nb_gaussian, # neighboorhood function
             neighborhood_init=som_size, # initial neighbourhood radius
             neighborhood_drate=0.0001) # decay of neighbourhood radius

perm = torch.randperm(x.size(0)) # to shuffle the data
for i in range(int(x.size()[0]/1000)):
    idx = perm[i*1000:(i+1)*1000] 
    time1 = time.time()
    dist,count = smodel.add(x[idx]) # feed the SOM a batch of 1000 pixels
    print(f"{(i+1):06d}K - {dist:.4f} - {(time.time()-time1)*1000:05.2f}ms")

The results on the image on the left looks like the map on the right, where each unit is represented by the colour corresponding to its weights.

map from image

Another example is included in the test/test_cheese.py creating a map of cheeses based on various binary attributes. The results is presented below with on the right the map represented by colours for each unit created through PCA with 3 components for the RGB components of the colour. On the left, a frequency map is given that show how many cheese have each unit for BMU (brighter == more cheese) as well as the name of the attribute most different in this unit compared to the average of the whole dataset.

map of chesses

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

ksom-0.9.2.tar.gz (24.3 MB view details)

Uploaded Source

Built Distribution

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

ksom-0.9.2-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file ksom-0.9.2.tar.gz.

File metadata

  • Download URL: ksom-0.9.2.tar.gz
  • Upload date:
  • Size: 24.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for ksom-0.9.2.tar.gz
Algorithm Hash digest
SHA256 8345da565d4505e3da49957026a7f6ab301ac58978f8851ac9af49a8006e8cba
MD5 60290c56d35ae68f4a7e954daabf909d
BLAKE2b-256 26ff185b6c57b48266743992058dcb4ec832c7684bcb571d04b286ef64e38041

See more details on using hashes here.

File details

Details for the file ksom-0.9.2-py3-none-any.whl.

File metadata

  • Download URL: ksom-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for ksom-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 27faf2fd851e38df99b63c3965dee3ffaa171a70c17c3dc62f1c8b48b64e6e9e
MD5 fe1c46cedf864ab1c8cf4385b2b7d509
BLAKE2b-256 87cf44668fd140de60f5b0200dfdcda568ad67d7d0eb14359a5ce05bfa533ee2

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