SelfOrganizingMap<T>

Models & Types · AiDotNet.Clustering.Neural

Self-Organizing Map (SOM) / Kohonen Network implementation.

Inheritance: ClusteringBase<T> → SelfOrganizingMap<T>

For Beginners

SOM creates a “neural map” of your data.

Think of it like training a room full of students:

  • Each student (neuron) specializes in a topic
  • Students sitting near each other learn similar topics
  • Over time, each student becomes expert in their area

The result is a 2D map where:

  • Each neuron represents a prototype pattern
  • Nearby neurons represent similar patterns
  • You can visualize high-dimensional data on a 2D grid

Great for:

  • Data visualization
  • Finding cluster structure
  • Understanding data topology

How It Works

A Self-Organizing Map is an unsupervised neural network that learns a low-dimensional representation of high-dimensional input data. The map preserves topological relationships: similar inputs activate nearby neurons.

Algorithm:

  1. Initialize neuron weights randomly
  2. For each training sample:

a. Find the Best Matching Unit (BMU) - closest neuron b. Update BMU and neighbors to be more like the input c. Decay learning rate and neighborhood radius over time

  1. Repeat for many iterations

Example

using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Clustering.Neural;
using AiDotNet.Tensors.LinearAlgebra;

var data = new Matrix<double>(6, 2);
double[][] rows = { new[] { 1.0, 1.0 }, new[] { 1.2, 0.9 }, new[] { 1.1, 1.1 },
                    new[] { 8.0, 8.0 }, new[] { 8.2, 7.9 }, new[] { 7.9, 8.1 } };
for (int i = 0; i < 6; i++) { data[i, 0] = rows[i][0]; data[i, 1] = rows[i][1]; }

var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new SelfOrganizingMap<double>())
    .ConfigureDataLoader(DataLoaders.FromMatrix(data))
    .BuildAsync();

var labels = result.Predict(data);
Console.WriteLine($"SelfOrganizingMap: clustered {labels.Length} points.");

Constructors

Constructor Summary
SelfOrganizingMap(SOMOptions<T> options) Initializes a new SOM instance.

Properties

Property Summary
Tensor<T> Weights { get; } Gets the neuron weight vectors (GridHeight x GridWidth x NumFeatures).
int[] NeuronLabels { get; } Gets the cluster label assigned to each neuron (GridHeight x GridWidth).

Methods

Method Summary
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters)
Matrix<T> GetUMatrix() Gets the U-Matrix (unified distance matrix) for visualization.
ModelOptions GetOptions()
ValueTuple<int, int> GetGridPosition(Vector<T> point) Gets the 2D grid position for a data point.
Vector<T> FitPredict(Matrix<T> x)
Vector<T> Predict(Matrix<T> x)
void Train(Matrix<T> x, Vector<T> y)