Graph-Based Clustering and Data Visualization in PythonThe graphclust toolbox

Chapter 3
Vector quantization and topology learning

Large data sets are expensive to turn into graphs, and noisy samples create spurious edges. Vector quantization replaces the samples by a few hundred representatives; topology learning additionally links the representatives so that the graph follows the shape of the data. This chapter starts with k-means and the Linde–Buzo–Gray algorithm, introduces neural gas and the topology representing network, and then presents the growing networks—growing neural gas, grow when required, the dynamic topology representing network, and the weighted incremental network—that do not need the number of representatives in advance. The graphs learned here are the input of TRNMap and the other topology-based maps of chapter 8.

3.1 Problem and idea

A vector quantizer maps every sample x to the nearest of n codebook vectors wi and thereby partitions the data space into Voronoi cells. Quantization reduces the number of objects from N to n, averages out noise within each cell, and makes the subsequent graph algorithms cheap: a geodesic distance matrix of n = 200 nodes is computed in milliseconds even if the data contain millions of samples. For clustering and mapping, however, the codebook alone is not enough; we also need to know which codebook vectors are neighbors along the data. Two nodes on opposite sides of a gap may be close in space, yet no data connect them.

Martinetz [42] answered this question with competitive Hebbian learning: for every sample, link its nearest and its second nearest node. The two nodes are then adjacent in the Voronoi diagram, and the point halfway between them lies in a region where data exist. The edges collected in this way form the induced Delaunay triangulation: the Delaunay triangulation of the nodes, masked by the data distribution (fig. 3.1). It is a much better graph than a kNN graph of the nodes, because it neither depends on a neighbor count nor bridges gaps.

Figure 3.1

Figure 3.1. Competitive Hebbian learning and the induced Delaunay triangulation. Linking the two nodes nearest to each sample keeps exactly those Delaunay edges whose Voronoi cells meet inside the data. Where the two arcs touch, the arcs are linked as well.

3.2 Theory

3.2.1 k-means and the Linde–Buzo–Gray algorithm

k-means [43, 44] minimizes the distortion J = ∑ ⁡ k min ⁡ i ‖xk −wi ‖ 2 by alternating two steps: assign every sample to its nearest codebook vector, and move every codebook vector to the mean of its samples. The algorithm converges to a local minimum that depends on the initial codebook, and codebook vectors that start in empty regions may never receive any sample. The Linde–Buzo–Gray (LBG) algorithm [45] reduces this sensitivity by growing the codebook: it starts with the centroid of all data, splits every codebook vector into two slightly perturbed copies, refines the doubled codebook with k-means iterations, and repeats until n vectors exist. k-means and LBG learn no graph; the package links their codebooks afterwards by competitive Hebbian learning on the data.

3.2.2 Neural gas

Neural gas [46, 47] replaces the hard assignment of k-means by a soft, rank-based one. In every step a sample x is drawn at random, the nodes are sorted by their distance to it, and node j with rank rj ∈{0,…,n −1} moves towards the sample by

wj ←wj + 𝜀(t)e−rj∕λ(t)(x −w j).
(3.1)

The step size 𝜀 and the neighborhood range λ decrease exponentially, g(t) = gi(gf∕gi)t∕tmax ⁡ . While λ is large, all nodes move together and the codebook first contracts to the center of the data; as λ shrinks, the update becomes more and more winner-take-all and the nodes spread over the data like a gas filling a container. Neural gas performs a stochastic gradient descent on a cost function that interpolates between a smooth, almost convex function and the k-means distortion, which explains why it is far less sensitive to the initial codebook than k-means.

3.2.3 Topology representing networks

A topology representing network (TRN) [42] runs neural gas and builds the graph by competitive Hebbian learning at the same time (algorithm 3.1). Because the nodes move during learning, early links may become invalid; every link therefore has an age that is reset whenever the link is confirmed and increased whenever its end node wins without confirming it. Links older than the lifetime T(t) are removed. Martinetz and Schulten proved that, for a sufficiently dense codebook, the resulting graph is the induced Delaunay triangulation, and it preserves the topology of the data manifold in the sense that neighboring nodes on the graph are neighbors on the manifold and vice versa.

Algorithm

The parameters follow eq. (3.1) plus the lifetime. With tmax ⁡ = 200n, 𝜀 from 0.3 to 0.05, λ from 0.2n to 0.01, and a lifetime growing from 0.1n to 0.5n the network is robust for most data; the number of nodes is by far the most influential choice.

3.2.4 Growing networks

A fixed n is a nuisance when the size of the structure is unknown. Growing networks start with one or two nodes and add nodes where the current representation is poor.

Growing neural gas (GNG). Fritzke’s algorithm [48] adapts only the winner s1 (step 𝜀b) and its graph neighbors (step 𝜀n), links s1 and s2 as in the TRN, accumulates the squared distance of each sample in an error variable of its winner, and every λ steps inserts a new node halfway between the node q with the largest error and its neighbor f with the largest error. The link q–f is replaced by links to the new node, and errors decay by a factor α at insertion and by d in every step. All parameters are constant in time, so GNG can run indefinitely on a data stream.

Grow when required (GWR). Marsland, Shapiro, and Nehmzow [49] insert a node as soon as it is needed rather than at fixed intervals. If the activity a = exp ⁡ (− ‖x −ws1‖) of the winner is below a threshold and the winner has already been trained enough—its habituation counter, which decays with every win, is low—a new node is placed between the winner and the sample. Otherwise the winner and its neighbors move with steps proportional to their habituation. GWR reacts quickly to new regions of the input space and is popular in robotics and continual learning.

Dynamic topology representing network (DTRN). Si, Lin, and Vuong [50] combine competitive Hebbian learning with a vigilance test. If the winner is farther than the vigilance threshold ρ(t) from the sample, the sample becomes a new node linked to the winner; otherwise the winner and its neighbors move towards the sample with Gaussian-normalized steps

Δwi = α(t) e−β ‖x−wi ‖2 ∑ j∈{s1}∪A(s1)e−β ‖x−wj ‖2(x −wi),i ∈{s1}∪A(s1),
(3.2)

where A(s1) are the graph neighbors of the winner, and the winner is linked to the second nearest node. Old links are removed together with the nodes they leave isolated. The final number of nodes is controlled by the vigilance schedule instead of a fixed count.

Weighted incremental neural network (WINN). Muhammed [51] extends GNG with weights that estimate density: every sample increments the weight of its nw nearest nodes and of the edges between the winner and the other nw −1 of them, and inserted nodes inherit averaged weights. Node and edge weights are later used for fuzzy clustering of the network. The paper recommends a maximal edge age of N∕10; on curved manifolds this keeps many transient links, and ages of a few steps give clean graphs (fig. 3.5).

In a nutshell: Topology learners

  • Neural gas places n nodes robustly by rank-based soft competition; the TRN adds competitive Hebbian links and yields the induced Delaunay triangulation.
  • GNG and WINN grow by error-driven insertion at fixed intervals, GWR when a sample is poorly represented, DTRN by a vigilance test; none needs n in advance, but each has a threshold that controls the final size.
  • Edge ages remove links created while the nodes were still moving; long lifetimes keep spurious links across gaps.
  • All produce a graph with Euclidean edge lengths that serves for geodesic distances, clustering, and mapping.

3.3 Using it in graphclust

graphclust.vq

NeuralGas(n_nodes, max_iter, eps=(0.3, 0.05), lam=None, init="range"), TopologyRepresentingNetwork(n_nodes, ..., lifetime=None, prune=True), GrowingNeuralGas(max_nodes, eps_b, eps_n, max_age, lambda_, alpha, d), GrowWhenRequired(activity_threshold, habituation_threshold, ...), DynamicTopologyRepresentingNetwork(max_iter, alpha, rho, max_age), WeightedIncrementalNetwork(n_nodes, n_winners, max_age, ...), LBG(n_nodes). All set nodes_, adjacency_ (Euclidean edge lengths), labels_, n_nodes_, n_edges_, and offer predict, transform, quantization_error, and graph_distances.

A topology representing network is fitted like any scikit-learn estimator:

    trn = TopologyRepresentingNetwork(n_nodes=n, random_state=0).fit(X) 
    
   

3.4 Examples

3.4.1 Neural gas on shapes of different dimension

The Boxlinecircle data of the authors combine a noisy cube, a bent line, and a circle (7100 samples). Figure 3.2 shows 300 neural gas nodes after increasing numbers of adaptation steps. After a hundred steps the nodes have contracted towards the middle of the data; after ten thousand steps they cover all three shapes, with a density that follows the data density. The quantization error falls from 1.93 to 0.57.

Figure 3.2

Figure 3.2. Neural gas on the Boxlinecircle data. The codebook first contracts and then spreads out until it covers the cube, the line, and the circle.

Open in ColabExample plot_01_neural_gas.py in the gallery

3.4.2 Topology representing networks of curved manifolds

Figure 3.3 shows TRNs of the S curve with 100 and 200 nodes and of the swiss roll with 200 nodes. The graphs follow the sheets: links connect nodes along the surface but not across the folds or between the windings. More nodes resolve the surface more finely.

Figure 3.3

Figure 3.3. Topology representing networks of the S curve and the swiss roll. Competitive Hebbian links stay on the surfaces.

Open in ColabExample plot_02_trn.py in the gallery

3.4.3 Growing networks

Figure 3.4 compares the four growing networks on the swiss roll scaled to the unit cube. GNG and WINN, which grow at fixed intervals up to a node budget, produce regular networks of about 200 nodes. GWR inserts nodes wherever a sample is poorly represented and stops when the activity threshold is met everywhere. DTRN creates many nodes early, while the vigilance threshold is still large relative to the node spacing, and prunes isolated ones later; its size depends on the vigilance schedule and the number of iterations.

Figure 3.4

Figure 3.4. Growing networks on the swiss roll. All four learn a graph on the sheet without being told the number of nodes; their final sizes are controlled by different thresholds.

Open in ColabExample plot_03_growing_networks.py in the gallery

3.4.4 Edge lifetime

The maximal edge age decides which transient links survive. Figure 3.5 trains WINN on the swiss roll with the age limit recommended in the original paper (N∕10 = 200) and with an age limit of three. The long lifetime keeps hundreds of links that jump between windings; the short one keeps only links along the sheet.

Figure 3.5

Figure 3.5. The maximal edge age of WINN. A long lifetime keeps links between the windings of the roll, a short lifetime removes them.

Open in ColabExample plot_04_edge_lifetime.py in the gallery

3.5 Further reading

The neural gas algorithm and its cost function are analyzed by Martinetz, Berkovich, and Schulten [47]; the topology preserving property of competitive Hebbian learning by Martinetz and Schulten [42]. Kohonen’s book [52] relates these methods to the self-organizing map of chapter 7. The growing networks are described in the original papers of Fritzke [48], Marsland et al. [49], Si et al. [50], and Muhammed [51].