Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Machine Learning & AI - Lab 2
Competitive learning for clustering, dimensionality reduction, and visualization of high-dimensional data
Input layer
Represents the features of the data
Output layer
A 2D grid of neurons; each neuron represents a cluster
How SOMs Work
Random weights
Every output neuron starts with randomly initialized weights
Compute distances
Euclidean distance from the input to every neuron's weight vector
BMU wins
The neuron with the smallest distance becomes the Best Matching Unit
w_{ij} is closest to the input feature x_i.alpha decays every epoch so the map convergesInitialize weights
Competition
Find the BMU
Weight update
Decay alpha
Check stopping
convergence)winner(): finds the BMU by comparing distances to each cluster's weightsupdate(): nudges the winning neuron's weights toward the sampleclass SOM:
def winner(self, weights, sample):
D0 = sum((sample[i]-weights[0][i])**2 for i in range(len(sample)))
D1 = sum((sample[i]-weights[1][i])**2 for i in range(len(sample)))
return 0 if D0 < D1 else 1
def update(self, weights, sample, J, alpha):
for i in range(len(weights[0])):
weights[J][i] += alpha * (sample[i] - weights[J][i])
return weights| Sample | Features |
|---|---|
| S1 | [1, 1, 0, 0] |
| S2 | [0, 0, 0, 1] |
| S3 | [1, 0, 0, 0] |
| S4 | [0, 0, 1, 1] |
10 epochs, initial learning rate alpha = 0.5BMU for every samplex0.9 after every epochfor epoch in range(epochs):
for j in range(m):
sample = T[j]
J = som.winner(weights, sample)
weights = som.update(weights, sample, J, alpha)
alpha *= 0.94 samples converge to a stable cluster assignment across the 10 epochsWorked Example
[0, 0, 0, 1] using the trained weights| Result | Value |
|---|---|
| Predicted cluster | 0 |
| Cluster 0 weights (approx.) | [0, 0, 0.57, 1.0] |
| Cluster 1 weights (approx.) | [1.0, 0.43, 0, 0] |
2 featuresalpha *= 0.5 vs 0.9): how does convergence change?3 or 4 clusters instead of 2BMU[1, 1, 1, 1] and observe cluster formationMahmoud Abas|Lab 2: Self-Organizing Maps (Kohonen Maps)