neupy.algorithms.competitive.sofm module

class neupy.algorithms.competitive.sofm.SOFM[source]

Self-Organizing Feature Map (SOFM or SOM).


n_inputs : int

Number of input units.

n_outputs : int or None

Number of outputs. Parameter is optional in case if feature_grid was specified.

learning_radius : int

Parameter defines radius within which we consider all neurons as neighbours to the winning neuron. The bigger the value the more neurons will be updated after each iteration.

The 0 values means that we don’t update neighbour neurons.

Defaults to 0.

std : int, float

Parameters controls learning rate for each neighbour. The further neighbour neuron from the winning neuron the smaller that learning rate for it. Learning rate scales based on the factors produced by the normal distribution with center in the place of a winning neuron and standard deviation specified as a parameter. The learning rate for the winning neuron is always equal to the value specified in the step parameter and for neighbour neurons it’s always lower.

The bigger the value for this parameter the bigger learning rate for the neighbour neurons.

Defaults to 1.

features_grid : list, tuple, None

Feature grid defines shape of the output neurons. The new shape should be compatible with the number of outputs. It means that the following condition should be true: == n_outputs

SOFM implementation supports n-dimensional grids. For instance, in order to specify grid as cube instead of the regular rectangular shape we can set up options as the following:

    features_grid=(5, 5, 5),

Defaults to (n_outputs, 1).

grid_type : {rect, hexagon}

Defines connection type in feature grid. Type defines which neurons we will consider as closest to the winning neuron during the training.

  • rect - Connections between neurons will be organized in hexagonal grid.
  • hexagon - Connections between neurons will be organized in hexagonal grid. It works only for 1d or 2d grids.

Defaults to rect.

distance : {euclid, dot_product, cos}

Defines function that will be used to compute closest weight to the input sample.

  • dot_product: Just a regular dot product between data sample and network’s weights
  • euclid: Euclidian distance between data sample and network’s weights
  • cos: Cosine distance between data sample and network’s weights

Defaults to euclid.

reduce_radius_after : int or None

Every specified number of epochs learning_radius parameter will be reduced by 1. Process continues until learning_radius equal to 0.

The None value disables parameter reduction during the training.

Defaults to 100.

reduce_step_after : int or None

Defines reduction rate at which parameter step will be reduced using the following formula:

step = step / (1 + current_epoch / reduce_step_after)

The None value disables parameter reduction during the training.

Defaults to 100.

reduce_std_after : int or None

Defines reduction rate at which parameter std will be reduced using the following formula:

std = std / (1 + current_epoch / reduce_std_after)

The None value disables parameter reduction during the training.

Defaults to 100.

weight : array-like, Initializer or {init_pca, sample_from_data}

Neural network weights. Value defined manualy should have shape (n_inputs, n_outputs).

Also, it’s possible to initialized weights base on the training data. There are two options:

  • sample_from_data - Before starting the training will randomly take number of training samples equal to number of expected outputs.
  • init_pca - Before training starts SOFM will applies PCA on a covariance matrix build from the training samples. Weights will be generated based on the two eigenvectors associated with the largest eigenvalues.

Defaults to Normal().

step : float

Learning rate, defaults to 0.1.

show_epoch : int or str

This property controls how often the network will display information about training. There are two main syntaxes for this property.

  • You can define it as a positive integer number. It defines how offen would you like to see summary output in terminal. For instance, number 100 mean that network shows summary at 100th, 200th, 300th ... epochs.
  • String defines number of times you want to see output in terminal. For instance, value '2 times' mean that the network will show output twice with approximately equal period of epochs and one additional output would be after the finall epoch.

Defaults to 1.

shuffle_data : bool

If it’s True class shuffles all your training data before training your network, defaults to True.

epoch_end_signal : function

Calls this function when train epoch finishes.

train_end_signal : function

Calls this function when train process finishes.

verbose : bool

Property controls verbose output interminal. True enables informative output in the terminal and False - disable it. Defaults to False.


  • Training data samples should have normalized features.


>>> import numpy as np
>>> from neupy import algorithms, environment
>>> environment.reproducible()
>>> data = np.array([
...     [0.1961, 0.9806],
...     [-0.1961, 0.9806],
...     [-0.5812, -0.8137],
...     [-0.8137, -0.5812],
... ])
>>> sofm = algorithms.SOFM(
...     n_inputs=2,
...     n_outputs=2,
...     step=0.1,
...     learning_radius=0
... )
>>> sofm.train(data, epochs=100)
>>> sofm.predict(data)
array([[0, 1],
       [0, 1],
       [1, 0],
       [1, 0]])


init_weights(train_data) Initialized weights besed on the input data. It works only for the init_pca and sample_from_data options. For other cases it will throw an error.
predict(input_data) Predicts output for the specified input.
train(input_train, summary=’table’, epochs=100) Train neural network.
fit(*args, **kwargs) Alias to the train method.
class DistanceParameter[source]

Alias for field number 1


Alias for field number 0

class SOFM.GridTypeMethods[source]

Alias for field number 1


Alias for field number 2


Alias for field number 0

SOFM.distance = None[source]
SOFM.features_grid = None[source]
SOFM.grid_type = None[source]
SOFM.learning_radius = None[source]
SOFM.n_outputs = None[source]
SOFM.options = {'std': Option(class_name='SOFM', value=NumberProperty(name="std")), 'distance': Option(class_name='SOFM', value=ChoiceProperty(name="distance")), 'reduce_radius_after': Option(class_name='SOFM', value=IntProperty(name="reduce_radius_after")), 'verbose': Option(class_name='Verbose', value=VerboseProperty(name="verbose")), 'weight': Option(class_name='SOFM', value=SOFMWeightParameter(name="weight")), 'reduce_step_after': Option(class_name='SOFM', value=IntProperty(name="reduce_step_after")), 'epoch_end_signal': Option(class_name='BaseNetwork', value=Property(name="epoch_end_signal")), 'shuffle_data': Option(class_name='BaseNetwork', value=Property(name="shuffle_data")), 'show_epoch': Option(class_name='BaseNetwork', value=ShowEpochProperty(name="show_epoch")), 'train_end_signal': Option(class_name='BaseNetwork', value=Property(name="train_end_signal")), 'learning_radius': Option(class_name='SOFM', value=IntProperty(name="learning_radius")), 'step': Option(class_name='BaseNetwork', value=NumberProperty(name="step")), 'reduce_std_after': Option(class_name='SOFM', value=IntProperty(name="reduce_std_after")), 'features_grid': Option(class_name='SOFM', value=TypedListProperty(name="features_grid")), 'n_inputs': Option(class_name='BaseAssociative', value=IntProperty(name="n_inputs")), 'n_outputs': Option(class_name='SOFM', value=IntProperty(name="n_outputs")), 'grid_type': Option(class_name='SOFM', value=ChoiceProperty(name="grid_type"))}[source]
SOFM.reduce_radius_after = None[source]
SOFM.reduce_std_after = None[source]
SOFM.reduce_step_after = None[source]
SOFM.std = None[source]
SOFM.train(input_train, summary='table', epochs=100)[source]

Method train neural network.


input_train : array-like

target_train : array-like or None

input_test : array-like or None

target_test : array-like or None

epochs : int

Defaults to 100.

epsilon : float or None

Defaults to None.

SOFM.train_epoch(input_train, target_train)[source]
SOFM.weight = None[source]