Neural Gas and GNG Networks in MATLAB
Train Neural Gas and Growing Neural Gas networks in MATLAB for prototype learning, clustering, and graph representations of data topology.

On this page
The Neural Gas network is a competitive Artificial Neural Network (ANN) proposed by Martinetz and Schulten in 1991. It is very similar to the Self-Organizing Map (SOM). Neural Gas can solve unsupervised learning tasks such as clustering, dimensionality reduction and topology learning. It is used in pattern recognition, data compression, speech recognition and image segmentation. For more information, see the Wikipedia article on Neural Gas (opens in a new tab).
In 1995, Bernd Fritzke proposed an extended version called the Growing Neural Gas (GNG) network. It starts with only 2 neurons and grows while the algorithm runs. The learning and adaptation mechanisms of GNG also differ slightly from those of the basic Neural Gas.
This post shares open-source MATLAB implementations of both the Neural Gas network and the Growing Neural Gas (GNG) network. The code is structured, so if you know MATLAB, you should find it easy to use in your research projects.
The problem
Both networks place a set of neurons, each with a weight vector in the data space, so that the neurons represent the distribution of the data. Each neuron then acts like a cluster center. Both networks also learn links between neurons that are close in the data, and these links show the shape (topology) of the data, for example a ring or a spiral.
Unlike the SOM, Neural Gas has no fixed grid. The neurons move freely, like the particles of a gas, and the links are learned from the data.
How Neural Gas works
For each input , the neurons are ranked by their distance to . The closest neuron has rank , the next one rank 1, and so on. Every neuron moves toward the input, and the step becomes smaller with the rank:
The step size and the neighborhood range decrease during training. In the code, they change exponentially from an initial to a final value over tmax steps:
The same rule is used for and for the age limit of the links. After each update, the code connects the two neurons closest to the input with a link and sets its age to zero. The other links of the closest neuron get older, and links older than are removed. This way of learning links is called competitive Hebbian learning.
How Growing Neural Gas works
GNG starts with two neurons at random positions and adds neurons during training. For each input :
- Find the nearest neuron and the second nearest .
- Increase the age of all links of , and add the squared distance to its local error.
- Move toward with the rate , and move its linked neighbors with the smaller rate .
- Connect and (or reset the age of their link to zero).
- Remove links older than , and remove neurons that have no links left.
- Every inputs, while the network has fewer than neurons, insert a new neuron halfway between the neuron with the largest error and its neighbor with the largest error. The errors of these two neurons are multiplied by , and the new neuron gets the new error of the first one.
- Multiply all errors by , so that old errors fade.
Because new neurons go where the error is large, the network grows into the regions that it does not yet represent well.
What is in the download
The download has two folders, 01 Neural Gas Network and 02 Growing Neural Gas Network. Each folder has:
NeuralGasNetwork.morGrowingNeuralGasNetwork.m: the training function,net = NeuralGasNetwork(X, params, PlotFlag).Xholds one sample per row,paramsis a structure with the parameters, andPlotFlagturns the live plot on or off. The resultnetholds the weightswand the link matrixC(and the errorsEfor GNG).app1.m,app2.mandapp3.m: three examples with the data sets inmydata.mat(circles),jain.mat(Jain) andspiral.mat(spiral).main.m: a dialog to choose one of the three examples.PlotResults.m: plots the data, the neurons and their links after each pass over the data.
How to run it
Open one of the two folders in MATLAB and run main.m, then choose an example. The plot is updated after every pass over the data, so you can watch the network learn the shape of the data. To use your own data, load it into X in one of the app scripts and adjust the parameters. The code calls pdist2 and unifrnd, so it probably needs the Statistics and Machine Learning Toolbox.
Parameters in the code
Neural Gas, set in app1.m, app2.m and app3.m:
| Parameter | Meaning | Circles | Jain | Spiral |
|---|---|---|---|---|
N |
Number of neurons | 30 | 40 | 100 |
MaxIt |
Passes over the data | 50 | 40 | 80 |
tmax |
Steps over which the parameters change | 10000 | 10000 | 8000 |
epsilon_initial, epsilon_final |
Step size | 0.4, 0.02 | 0.5, 0.01 | 0.9, 0.4 |
lambda_initial, lambda_final |
Neighborhood range | 2, 0.1 | 5, 0.5 | 10, 1 |
T_initial, T_final |
Age limit of links | 5, 10 | 5, 10 | 5, 10 |
Growing Neural Gas:
| Parameter | Meaning | Circles | Jain | Spiral |
|---|---|---|---|---|
N |
Maximum number of neurons | 50 | 40 | 100 |
MaxIt |
Passes over the data | 20 | 20 | 50 |
L |
Inputs between two insertions | 50 | 50 | 100 |
epsilon_b |
Step size of the winner | 0.2 | 0.2 | 0.8 |
epsilon_n |
Step size of its neighbors | 0.005 | 0.005 | 0.01 |
alpha |
Error reduction after an insertion | 0.5 | 0.5 | 0.5 |
delta |
Error decay factor | 0.995 | 0.995 | 0.995 |
T |
Age limit of links | 50 | 20 | 10 |
Check the result
Inspect both prototype positions and the learned links rather than treating every neuron as a confirmed natural cluster. Scale features before distance-based training and compare several runs because initialization and sample order can affect the result. For Growing Neural Gas, the maximum neuron count is a stopping cap, not an estimate of the true cluster count. Evaluate the representation on separate data if it will feed a later prediction task.
Related posts
- Evolutionary Data Clustering in MATLAB
- DBSCAN Clustering in MATLAB
- Intelligent Image Color Reduction and Quantization, which uses a Self-Organizing Map for clustering.
Downloads
How to cite
If you use this code or tutorial in your work, articles or publications, please cite it.
Mostapha Kalami Heris, Neural Gas and GNG Networks in MATLAB (YPML111), Yarpiz, 2015. https://yarpiz.com/neural-gas-network-ypml111@misc{yarpiz-ypml111,
author = {{Mostapha Kalami Heris}},
title = {{Neural Gas and GNG Networks in MATLAB}},
howpublished = {Yarpiz},
year = {2015},
note = {Code YPML111},
url = {https://yarpiz.com/neural-gas-network-ypml111}
}Published Updated
Comments