SOMGpu — GPU Implementation

SOMGpu mirrors the SOM API but offloads computation to the GPU via CuPy. It is selected automatically by SOMPlots when CuPy is available.

from NNSOM.som_gpu import SOMGpu   # requires CuPy

som = SOMGpu((8, 8))
som.init_w(data)
som.train(data, init_neighborhood=3, epochs=200, steps=100)

Note

CuPy requires an NVIDIA CUDA-capable GPU. If CuPy is not installed, use SOM or SOMPlots instead — they fall back to NumPy automatically.

class NNSOM.som_gpu.SOMGpu(dimensions)[source]

Bases: object

Represents a Self-Organizing Map (SOM) using GPU acceleration with CuPy.

A Self-Organizing Map (SOM) is an artificial neural network used for unsupervised learning, which projects high-dimensional data into a lower-dimensional (typically two-dimensional) space. It is trained using a competitive learning approach to produce a discretized representation of the input space of training samples.

dimensions

Dimensions of the SOM grid, defining the layout and number of neurons.

Type:

tuple, list, np.ndarray

numNeurons

Total number of neurons, computed as the product of the grid dimensions.

Type:

int

pos

Positions of neurons within the grid.

Type:

np.ndarray

neuron_dist

Precomputed Euclidean distances between neurons in the grid.

Type:

np.ndarray

w

Weight matrix representing the feature vectors of the neurons.

Type:

np.ndarray

sim_flag

Indicates if the SOM has been simulated/trained.

Type:

bool

output

Output from the latest simulation.

Type:

np.ndarray

norm_func

Function used to normalize input data.

Type:

callable

sub_som

Optional sub-clustering using additional SOMs at neuron positions.

Type:

dict

__init__(self, dimensions)[source]

Initializes the SOM with the specified dimensions.

init_w(self, x, norm_func=None)[source]

Initializes the weights using PCA on input data x.

sim_som(self, x)[source]

Simulates SOM processing for input x, identifying activated neurons.

train(self, x, init_neighborhood=3, epochs=200, steps=100, norm_func=None)[source]

Trains the SOM using batch SOM algorithm on input data x.

quantization_error(self, dist)[source]

Calculates the quantization error of the model.

topological_error(self, data)[source]

Calculates the topological error of the model.

distortion_error(self, data)[source]

Calculates the distortion error of the model.

save_pickle(self, filename, path, data_format='pkl')[source]

Saves the SOM object to a file in pickle format.

load_pickle(self, filename, path, data_format='pkl')[source]

Loads the SOM object from a file in pickle format.

_normalize_position(self, position)[source]

Helper method to normalize neuron positions.

_spread_positions(self, position, positionMean, positionBasis)[source]

Helper method to adjust neuron positions.

_euclidean_distance(self, XA, XB)[source]

Computes Euclidean distances between two sets of vectors.

_to_categorical(self, x, num_classes=None)[source]

Converts class vector to binary class matrix.

Raises:

ImportError – If CuPy is not available, suggests using the NNSOM package for a NumPy-based implementation.

Example

>>> dimensions = (10, 10)
>>> som = SOMGpu(dimensions)
>>> data = np.random.rand(100, 10)
>>> som.init_w(data, norm_func=None)
>>> som.train(data, norm_func=None)
>>> output = som.sim_som(data)

Initialization and training

SOMGpu.init_w

Initializes the weights of the SOM using principal components analysis (PCA) on the input data x.

SOMGpu.train

Trains the SOM using the batch SOM algorithm on the input data x.

SOMGpu.sim_som

Simulates the SOM with x as the input, determining which neurons are activated by the input vectors.

Clustering

SOMGpu.cluster_data

Cluster the input data based on the trained SOM reference vectors.

Quality metrics

SOMGpu.quantization_error

Calculate quantization error

SOMGpu.topological_error

Calculate topological error

SOMGpu.distortion_error

Calculate distortion

Persistence

SOMGpu.save_pickle

Save the SOM object to a file using pickle.

SOMGpu.load_pickle

Load the SOM object from a file using pickle.