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:
objectRepresents 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.
- 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
- output
Output from the latest simulation.
- Type:
np.ndarray
- norm_func
Function used to normalize input data.
- Type:
callable
- 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.
- 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.
- _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.
- 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
Initializes the weights of the SOM using principal components analysis (PCA) on the input data x. |
|
Trains the SOM using the batch SOM algorithm on the input data x. |
|
Simulates the SOM with x as the input, determining which neurons are activated by the input vectors. |
Clustering
Cluster the input data based on the trained SOM reference vectors. |
Quality metrics
Calculate quantization error |
|
Calculate topological error |
|
Calculate distortion |
Persistence
Save the SOM object to a file using pickle. |
|
Load the SOM object from a file using pickle. |