Skip to main content

fastersom

A small, fast batch self-organising map (SOM) in PyTorch: a reimplementation of Kohonen's batch map with a rectangular grid, gaussian neighbourhood and exponentially decaying neighbourhood width. Its defaults reproduce the maps of spatiomic's so.dimension.som. Data stays on the device (CUDA, MPS or CPU) and the per-epoch update uses per-node sums, so an epoch costs one BMU search plus a (nodes x nodes) matrix product.

Install

uv add fastersom    # or: pip install fastersom

Usage

import numpy as np
from fastersom import SOM

X = np.load("pixels.npy")  # (n, d) or (..., d); numpy, array-like or torch
som = SOM(n_rows=30, n_cols=30, n_epochs=10, random_state=0)  # device: cuda > mps > cpu
som.fit(X)
som.weights_  # (900, d) node weights, node = row * n_cols + col
som.labels_  # BMU index per training sample
labels = som.predict(X)  # BMU index per sample
dist = som.transform(X)  # (n, 900) distance to every node
print(som.quantization_error(X), som.topographic_error(X))

Options:

  • metric="cosine" finds BMUs by cosine similarity. Zero-norm samples have no BMU, get label -1 and are left out of the update and the error measures.
  • init="sample" initialises the nodes from random samples instead of seeded random unit vectors.
  • sigma_initial and sigma_final set the neighbourhood width schedule; the defaults reproduce spatiomic.
  • batch_size bounds memory (about batch_size x n_nodes floats) without changing the algorithm.

BatchSOM is an alias for SOM.

Benchmark

480,000 x 34 pixels, 30 x 30 nodes, 10 epochs, fit time only (mean of seeds 0-2):

implementation hardware fit time quantization error
fastersom NVIDIA A100 0.085 s 0.169
spatiomic (CuPy) NVIDIA A100 0.46 s 0.169
spatiomic (NumPy) 256-core CPU 40 s 0.169

Algorithm

fastersom follows Kohonen's batch map (T. Kohonen, Self-Organizing Maps, 3rd ed., Springer, 2001). Each epoch assigns every sample to its best matching unit and sets every node to the neighbourhood-weighted mean of the samples, with neighbourhood h(j, k) = exp(-||r_j - r_k||^2 / (2 sigma(t)^2)) on the grid coordinates r and sigma(t) = sigma_initial * (sigma_final / sigma_initial) ** (t / n_epochs). There is no learning rate, because it cancels in the batch update. Only the rectangular topology, gaussian neighbourhood and exponential decay are supported.

Development

uv sync                      # installs the dev group: pytest, ruff, pre-commit, twine
uv run pre-commit install    # ruff, uv-lock and file checks on every commit
uv run pytest
uv build && uv run twine check --strict dist/*

License

Apache-2.0

Metadata

Release files for fastersom 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastersom 0.1.1
File Size Uploaded Provenance
fastersom-0.1.1.tar.gz 11.6 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for fastersom 0.1.1
File Interpreter ABI Platform
fastersom-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 23.6 kB

Release files / fastersom-0.1.1.tar.gz

Download URL fastersom-0.1.1.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ed12b990957428ef0a3ad673ab7dbf8a310032f4b0cf1dd0627e54a88c4df063
BLAKE2b-256 checksum
How to use checksums
4c2703dc5cce52ecea284c3970c2f066b2a9b0e75e5dc13b6fc852d2afa90e4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / fastersom-0.1.1-py3-none-any.whl

Download URL fastersom-0.1.1-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
88c9c6088cecc198cfb903a401ac2d579c861ef2649980fdfea338d162ea1418
BLAKE2b-256 checksum
How to use checksums
30c6686253d9d9ef412c0bdc4e318e8202ff440e632dce27637b55f6b72d2b99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Security for fastersom 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fastersom-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page