Skip to main content

Self-Organizing Maps in python

Project description

Somap

Somap is a flexible, fast and scalable Self-Organizing Maps library in python. It allows you to define and run different flavors of SOMs (Kohonen, DSOM or your custom ones) on square or hexagonal 2D grid, with or without toroidal topology.

    

Why a new SOM library?

There are already a few open-source libraries for Self-Organizing Maps in python, of which MiniSom and SOMPY seem to be the most popular. I developped Somap to overcome what I believe to be two shortcomings of existing libraries for my research on bio-inspired AI:

  • Ability to easily customize the SOM algorithm (e.g. distance, neighborhood, learning rate and update functions).
  • Capacity to vectorize the computations over many SOMs (e.g. for distributed learning over 2D maps of SOMs).

Thanks to JAX's jit and vmap functions, it turned out that performance was also significantly better compared to other frameworks. Under the hood, it relies indirectly on JAX via the Equinox library that offers an easy-to-use PyTorch-like syntax.

Installation

Requires Python 3.10+ and a working installation of JAX 0.4.20+. You can follow these instructions to install JAX with the relevant hardware acceleration support. I am currently working on different ways to extend the basic SOM algorithm:

pip install somap

Quick example

The classic workflow goes as follow:

import somap as smp

# Load the MNIST dataset as a Numpy array of shape (60000, 28, 28)
data = smp.datasets.MNIST().data

# Initialize the 2D map
model = smp.StaticKsom(
    shape = (11, 13), 
    topography = "hex", 
    borderless = False, 
    input_shape = (28, 28), 
    params = smp.StaticKsomParams(sigma=0.3, alpha=0.5)
)

# Train (see documentation to understand the "bu_v" dict key)
model, aux = smp.make_steps(model, {"bu_v": data})

# Plot the 2D map 
smp.plot(model)

# Retrieve the errors from all steps
quantization_errors = aux["metrics"]["quantization_error"]
topographic_errors = aux["metrics"]["topographic_error"]

You can also define your custom SOM:

import somap as smp
from jaxtyping import Array, Float

class MyCustomSomParams(smp.AbstractSomParams):
    sigma: float | Float[Array, "..."]
    alpha: float | Float[Array, "..."]

class MyCustomSom(smp.AbstractSom):

    @staticmethod
    def generate_algo(p: MyCustomSomParams) -> smp.SomAlgo:
        return smp.SomAlgo(
            f_dist=smp.EuclidianDist(),
            f_nbh=smp.GaussianNbh(sigma=p.sigma),
            f_lr=smp.ConstantLr(alpha=p.alpha),
            f_update=smp.SomUpdate(),
        )

If you need custom distance, neighborhood, learning rate and update functions for your SOM, you can define them by inheriting from smp.AbstractDist, smp.AbstractNbh, smp.AbstractLr and smp.AbstractUpdate. See the library source code for how to do it.

Documentation

See: https://mthiboust.github.io/somap/

Next steps

I am currently working on different ways to extend the basic SOM algorithm:

  • Inputs: In addition to classic bottom-up driving inputs, a SOM could also receive lateral contextual or top-down modulatory inputs.
  • Weighted inputs: Each data point from inputs can be weighted so that fuzzy data is weighted less for the winner selection.
  • Dynamics: When receiving continuous inputs in time, past activations can influence the computation of the next step.
  • Supervised and self-supervised learning: Top-down inputs and next inputs in time can act as teaching signal for supervised and self-supervised learning.
  • Multi-agent system: Each SOM is an agent of a mutli-agent system where thousands of SOMs interact with each other.

Some of these features will land on an other library that depends on Somap.

Citation

If you found this library to be useful in academic work, then please cite:

@misc{thiboust2023somap,
  title={Somap: a flexible, fast and scalable python library for Self-Organizing Maps.},
  author={Matthieu Thiboust},
  year={2023},
  url={https://github.com/mthiboust/somap/},
}

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

somap-0.1.1.tar.gz (293.6 kB view details)

Uploaded Source

Built Distribution

somap-0.1.1-py3-none-any.whl (295.9 kB view details)

Uploaded Python 3

File details

Details for the file somap-0.1.1.tar.gz.

File metadata

  • Download URL: somap-0.1.1.tar.gz
  • Upload date:
  • Size: 293.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.15.1 CPython/3.10.12 Linux/6.5.0-1018-azure

File hashes

Hashes for somap-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b7a0142d5f36c98e9211501fa582d3f8cca0c5fa7998a0ba4378db1779eb0e72
MD5 ad9d11d129690da1314aea0af48b7685
BLAKE2b-256 d0f4eeae4a6d1ed1ba58a7d46f6d30cfc6ed912400827019efbd13ca1b8e3c0b

See more details on using hashes here.

File details

Details for the file somap-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: somap-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 295.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.15.1 CPython/3.10.12 Linux/6.5.0-1018-azure

File hashes

Hashes for somap-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a81b4b0911ffa5b56fef59d30cbbc6bbb5f3bc45187472247fa5edbb2ee5b7ef
MD5 76eb55c1bc0fc717ae6559a1d5b6c82e
BLAKE2b-256 5742c0cdbe1ceaa43ae47fc98d4dd3e901242d53e5889ebd09be1c9d6e6ad093

See more details on using hashes here.

Supported by

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