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.
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ksom-0.9.3.tar.gz.
File metadata
- Download URL: ksom-0.9.3.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
616740e9956122e908b7b193fdeb6771d361b4147b9f27aae09c6c48db79c0b9
|
|
| MD5 |
128feabdffabb04a28ffd7d64154633d
|
|
| BLAKE2b-256 |
968ae5e88bb2b88b810b2bd44ed35d6c576747cb30448ef200541aa348398894
|
File details
Details for the file ksom-0.9.3-py3-none-any.whl.
File metadata
- Download URL: ksom-0.9.3-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8bf55483d0b6493396fab3a9f7d51c5beacb123cf4c22763e1f40c1ccd7f84e7
|
|
| MD5 |
72158af0fa84b78ff68f85d6d4c96127
|
|
| BLAKE2b-256 |
52a9aefb1001e38ce621b8406b6083f5f5c9fe2bd13b9f5b615c8275889dc1a2
|