Class SelfOrganizingMap<T>
- Namespace
- AiDotNet.NeuralNetworks
- Assembly
- AiDotNet.dll
Represents a Self-Organizing Map, which is an unsupervised neural network that produces a low-dimensional representation of input data.
public class SelfOrganizingMap<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, INeuralNetwork<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable
Type Parameters
TThe numeric type used for calculations, typically float or double.
- Inheritance
-
SelfOrganizingMap<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
A Self-Organizing Map (SOM), also known as a Kohonen map, is a type of artificial neural network that uses unsupervised learning to produce a low-dimensional (typically two-dimensional) representation of higher-dimensional input data. SOMs preserve the topological properties of the input space, meaning that similar inputs will be mapped to nearby neurons in the output map. This makes SOMs useful for visualization, clustering, and dimensionality reduction of complex data.
For Beginners: A Self-Organizing Map is like a smart way to arrange data on a map based on similarities.
Think of it like organizing books on a bookshelf:
- You have many books (input data) with different characteristics
- You want to arrange them so similar books are placed near each other
- Over time, you develop a system where sci-fi books are in one section, romance in another, etc.
A SOM works in a similar way:
- It takes complex data with many attributes
- It creates a 2D "map" where each location represents certain characteristics
- Similar data points end up mapped to nearby locations
- Different regions of the map represent different types of data
This is useful for:
- Visualizing complex data with many dimensions
- Finding natural groupings (clusters) in data
- Reducing complex data to simpler patterns
- Discovering relationships that might not be obvious
Constructors
SelfOrganizingMap(NeuralNetworkArchitecture<T>, int, ILossFunction<T>?)
Initializes a new instance of the SelfOrganizingMap<T> class with the specified architecture.
public SelfOrganizingMap(NeuralNetworkArchitecture<T> architecture, int totalEpochs = 1000, ILossFunction<T>? lossFunction = null)
Parameters
architectureNeuralNetworkArchitecture<T>The neural network architecture to use for the SOM.
totalEpochsintlossFunctionILossFunction<T>
Remarks
This constructor creates a new Self-Organizing Map based on the provided architecture. It extracts the input dimension from the architecture's input size and calculates appropriate map dimensions (width and height) based on the architecture's output size. It attempts to make the map as square as possible for better visualization and organization.
For Beginners: This sets up the Self-Organizing Map with its basic structure.
When creating a new SOM:
- The architecture tells us how many input dimensions we have (how many attributes each data point has)
- The architecture also suggests how many total positions we want on our map
- The constructor tries to make the map as square as possible (e.g., 10×10 rather than 5×20)
- It may adjust the total map size slightly to make a perfect square if needed
Once the dimensions are set, it creates weight values for each position on the map. These weights are initially random and will be adjusted during training.
Exceptions
- ArgumentException
Thrown when input dimension or output size is invalid.
Methods
CreateNewInstance()
Creates a new instance of the Self-Organizing Map with the same configuration.
protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()
Returns
- IFullModel<T, Tensor<T>, Tensor<T>>
A new instance of the Self-Organizing Map.
Remarks
This method creates a deep copy of the current Self-Organizing Map, including its architecture, weights, and training progress. The new instance is completely independent of the original, allowing modifications without affecting the original.
For Beginners: This method creates an exact copy of the current SOM.
The copy includes:
- The same map dimensions (width and height)
- The same input dimension
- The same weights for all positions on the map
- The same training progress (current epoch)
This is useful when you want to:
- Create a backup before continuing training
- Create variations of the same map for different purposes
- Share the map while keeping your original intact
Exceptions
- InvalidOperationException
Thrown when the creation fails or required components are null.
DeserializeNetworkSpecificData(BinaryReader)
Deserializes the specific data of the Self-Organizing Map.
protected override void DeserializeNetworkSpecificData(BinaryReader reader)
Parameters
readerBinaryReaderThe BinaryReader to read the data from.
Remarks
This method reads SOM-specific data from a binary stream and reconstructs the map's state. It restores the map dimensions, training parameters, and the weights of all neurons in the map.
For Beginners: This method loads all the important information about a previously saved SOM.
It loads:
- The number of input dimensions
- The width and height of the map
- The total number of training epochs
- The current training epoch
- All the weights for every position on the map
This allows a previously saved SOM to be fully restored to continue training or make predictions.
GetModelMetadata()
Gets the metadata of the Self-Organizing Map model.
public override ModelMetadata<T> GetModelMetadata()
Returns
- ModelMetadata<T>
A ModelMetaData object containing information about the SOM.
Remarks
This method returns metadata about the SOM, including its type, dimensions, and training progress. It also includes serialized model data, which can be used to save or transfer the model state.
For Beginners: This method provides a summary of the SOM's current state.
The metadata includes:
- The type of model (Self-Organizing Map)
- The number of input dimensions
- The width and height of the map
- The total number of training epochs planned
- The current number of training epochs completed
- A serialized version of the entire model (useful for saving or sharing the model)
This information is useful for keeping track of the model's configuration and training progress.
InitializeLayers()
Initializes the neural network layers. In a SOM, this method is typically empty as SOMs use direct weight and map parameters rather than standard neural network layers.
protected override void InitializeLayers()
Remarks
SOMs differ from feedforward neural networks in that they don't use a layer-based computation model. Instead, they directly manipulate weights and use a competitive learning approach where neurons compete to respond to input patterns. Therefore, this method is typically empty or performs specialized initialization for SOMs.
For Beginners: SOMs work differently from standard neural networks.
While standard neural networks process data through sequential layers:
- SOMs use a competitive approach where neurons "compete" to respond to input
- They don't use the same layer concept as feedforward networks
- They operate directly on the weights connecting the input to the map neurons
That's why this method is empty - the SOM initializes its weights directly rather than creating a sequence of layers like a standard neural network.
Predict(Tensor<T>)
Predicts the output for a given input using the trained Self-Organizing Map.
public override Tensor<T> Predict(Tensor<T> input)
Parameters
inputTensor<T>The input tensor to predict.
Returns
- Tensor<T>
A tensor representing the prediction result.
Remarks
This method finds the Best Matching Unit (BMU) for the given input and returns a one-hot encoded tensor representing the BMU's position in the map. The output tensor has a size equal to the total number of neurons in the map (_mapWidth * _mapHeight).
For Beginners: This method finds the best matching position on the map for new input data.
When predicting:
- It checks if the input data has the correct number of attributes
- It finds the position on the map that best matches the input (the BMU)
- It creates an output where only the BMU position is marked as active (1), and all others are inactive (0)
This output tells you which part of the map best represents the input data, which can be used for classification, clustering, or visualization.
Exceptions
- ArgumentException
Thrown when the input shape doesn't match the expected input dimension.
SerializeNetworkSpecificData(BinaryWriter)
Serializes the specific data of the Self-Organizing Map.
protected override void SerializeNetworkSpecificData(BinaryWriter writer)
Parameters
writerBinaryWriterThe BinaryWriter to write the data to.
Remarks
This method writes the SOM-specific data to a binary stream. It includes the map dimensions, training parameters, and the weights of all neurons in the map.
For Beginners: This method saves all the important information about the SOM.
It saves:
- The number of input dimensions
- The width and height of the map
- The total number of training epochs
- The current training epoch
- All the weights for every position on the map
This allows the entire state of the SOM to be saved and later restored exactly as it was.
Train(Tensor<T>, Tensor<T>)
Trains the Self-Organizing Map using the provided input.
public override void Train(Tensor<T> input, Tensor<T> expectedOutput)
Parameters
inputTensor<T>The input tensor to train on.
expectedOutputTensor<T>The expected output tensor (not used in SOM training).
Remarks
This method performs one training step for the SOM. It finds the Best Matching Unit (BMU) for the input, calculates the current learning rate and neighborhood radius, and updates the weights of the neurons in the BMU's neighborhood. The learning rate and radius decrease over time to refine the map's organization.
For Beginners: This method adjusts the map based on new input data.
During training:
- It checks if the input data has the correct number of attributes
- It finds the best matching position (BMU) for the input
- It calculates how much the map should change (learning rate) and how far the change should spread (radius)
- It updates the map, with the BMU and nearby positions becoming more like the input data
- It keeps track of how many times the map has been trained (epochs)
Over time, this process organizes the map so that similar inputs activate nearby positions.
Exceptions
- ArgumentException
Thrown when the input shape doesn't match the expected input dimension.
UpdateParameters(Vector<T>)
Updates the parameters of the SOM from a flat parameter vector.
public override void UpdateParameters(Vector<T> parameters)
Parameters
parametersVector<T>The vector of parameter updates to apply.
Remarks
This method updates the SOM's weight matrix from a flat parameter vector. The parameter vector must have a length equal to (mapWidth × mapHeight) × inputDimension. While SOMs typically use specialized training algorithms (see the Train method), this method allows for direct parameter updates, which can be useful for optimization algorithms or parameter transfer scenarios.
For Beginners: This method allows direct parameter updates when needed.
While SOMs typically use competitive learning:
- SOMs use a competitive learning approach
- They update based on neighborhood and distance
- They directly adjust weights based on similarity to input
However, this method allows direct parameter updates for certain optimization algorithms or parameter transfer scenarios. For typical SOM training, use the Train method instead.
Exceptions
- ArgumentException
Thrown when the parameter vector length doesn't match the expected number of weights.