This optional lecture continues the original STAT 432 discussion of self-organizing maps.
Basic Concepts
I found the best demonstration of the self-organizing map algorithm is the following graph that displays it over iterations. It is available at this website.
A self-organizing grid moves toward observations over successive updates. The connected black centers gradually spread through the observed point cloud.
Let’s understand this by pairing it with the algorithm. There are several different algorithms available, but one of the most popular is Kohonen’s self-organizing map. Here, we present a SOM with a two-dimensional output. The following are the inputs:
is a set of observations, each with dimension (the yellow and green dots in the figure).
, , , are a grid of centers (the connected black dots). They are similar to the centers in a K-means algorithm. However, the grid also encourages neighboring centers to represent similar observations. Grid location and location in the original -dimensional feature space are different quantities.
is a learning rate. It controls how fast the ’s are updated.
is a neighborhood radius. It controls which grid centers are updated at each iteration.
Now we look at the algorithm. This is different from K-means because we do not use all the observations immediately. The algorithm works by streaming in observations one by one. Whenever an observation comes in, we update the centers as follows:
For every , calculate . We use Euclidean distance in the feature space.
Select the closest center, with grid location . This is the best matching unit.
Update each center using the vector difference:
The difference gives both the direction and distance of the move. With a bubble neighborhood, is 1 for units within grid distance of the winner and 0 otherwise. Nearby grid units therefore move together toward the incoming observation.
After each update, we decrease and . In the kohonen package’s online algorithm, decreases linearly from 0.05 toward 0.01. The initial radius is the two-thirds quantile of all pairwise grid distances, then decreases toward zero; when the radius falls below 1, the package uses 0.5 so only the winning unit moves. An epoch contains updates, each using an observation sampled with replacement. These are the settings used below. See the kohonen reference manual for the package conventions.
Example: Handwritten Digit Data
Using the kohonen package, we perform a SOM on the handwritten digit recognition data. For speed, the original example uses only ten of the 256 pixel measurements. The training file contains 7,291 observations, with the digit in the first column and the pixels in the remaining columns. We use the supplied pixel values without additional standardization.
Both languages use the original ten column indices, a rectangular grid, 100 epochs, the bubble neighborhood, and the same initial observation rows. The rectangular grid uses maximum-coordinate (Chebyshev) distance between unit locations, so diagonal neighbors are included. The grid coordinates are supplied so that unit order agrees across languages.
The following function implements the online update above. Numba compiles the numerical loop; the data, winner search, learning-rate schedule, and neighborhood updates remain explicit.
The maps need not have the same orientation or unit assignments across runs. The random observation streams differ across languages, even with equal seed numbers. Both routes fit the same online SOM model with the same data, starting centers, grid, and update schedule.
The neighbor-distance plot shows how far each center is from its neighboring centers. A large value means the center differs strongly from adjacent units. Here the plotted quantity is the mean squared Euclidean feature distance to units at grid distance 1. Orange marks larger distances. The mapping plot places each observation at its winning grid unit, with a little jitter to reveal how many observations occupy it.
som_palette <-colorRampPalette(c("#F2F5F8", "#E7B494", orange))plot(zip.SOM, type ="dist.neighbours", palette.name = som_palette,shape ="straight", border ="white", main ="Neighbor distances")
Average squared distance from each SOM prototype to its neighboring prototypes.
Show the reproducible code
set.seed(2)plot(zip.SOM, type ="mapping", pchs =20, col =adjustcolor(navy, 0.3),cex =0.35, shape ="straight", border ="#A0ACBD", main ="Mapping Type SOM")
The 7,291 observations mapped to their best matching SOM units.
The 7,291 observations mapped to their best matching SOM units.
The ten selected pixels make the example fast, but discard much of the image information. You can try using all 256 pixels by replacing the selected columns with zip.train[, 2:257] in R or zip_train[:, 1:257] in Python, then initializing and fitting again. Neither of these unsupervised fits uses the true digit labels.
A small supervised extension
We could also incorporate the class labels (digits) when constructing the map. The original xyf example does this using a hexagonal grid and 300 epochs. This is a supervised extension: labels enter the fit, so this is a different model from the unsupervised SOM above.
There are now two prototype layers, one for pixels and one for a ten-dimensional one-hot digit vector. We retain kohonen’s squared Euclidean distance for pixels and its default Tanimoto dissimilarity for the class layer. Here Tanimoto is the fraction of class coordinates with different thresholded states at 0.5, using the package’s threshold convention. The Python update includes this same calculation. Each layer receives equal importance after normalization by its mean pairwise dissimilarity among the initial prototypes.
profile_colors <-c("#13294B", "#C84A16", "#75969E", "#A58B59", "#6F668C","#5D7B51", "#A2596D", "#6695B5", "#C28650", "#657381")oldpar <-par(mfrow =c(1, 2))plot(zip.SOM2, type ="codes", main =c("Pixel prototypes", "Digit class prototypes"),palette.name =function(n) profile_colors[seq_len(n)], shape ="straight",border ="#A0ACBD")add.cluster.boundaries(zip.SOM2, zip.SOM2.hc, col = navy, lwd =2)
Pixel prototype profiles (left) and digit-class prototype profiles (right) for the supervised map. Boundaries group the class prototypes by complete linkage.
Pixel prototype profiles (left) and digit-class prototype profiles (right) for the supervised map. Boundaries group the class prototypes by complete linkage.
The radial segments show the fitted prototype coordinates, rescaled within each coordinate for display. In the right panel, they describe digit-class prototype weights learned during training. They are not empirical proportions of the digits assigned to a unit. The boundaries come from hierarchical clustering of those class prototype vectors. They do not provide an independent assessment of classification accuracy.