-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.m
182 lines (107 loc) · 4.81 KB
/
main.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
%% Self-organizing maps are similar to PCA and they are easier to understand
% We'll be using self-organizing maps to evaluate the alignment of red dots
% to each other as it changes with higher levels of sebum in the image
%
%% Initialization
clear ; close all;
%clf; shg;
%% =========== Step 0: Loading and Visualizing Image Randomly =============
load Aggregation.txt;
train_data = Aggregation(:,1:2);
y = Aggregation(:,3);
%plotData(train_data,y);
% dataRow is the number of training example
% dataCol is the number of features in each training example
[dataRow, dataCol] = size(train_data);
%% =========== Define the SOM Architecture ================================
% Determine the number of rows and columns in the self-organizing map
somRow = 20;
somCol = 20;
% Number of iteration for convergence
Iteration = 10000;
%%=========== Parameter Setting For SOM ===================================
% Initial size of topological neighbourhood of the winning neuron
width_Initial = 15;
% Time constant for initial topological neighbourhood size
t_width = Iteration/log(width_Initial);
% Initial time-varying learning rate
learningRate_Initial = 1;
% Time constant for the time-varying learning rate
t_learningRate = Iteration;
%% =========== Step 1: Initialize The Weight Of Neurons Randomly =========
fprintf('\nInitializing The Weight Of Neurons Randomly ...\n')
% Initial weight vector of neuron
somMap = randInitializeWeights(somRow,somCol,dataCol);
% Plot the training data
plotData(train_data, y);
hold on;
%% =========== Step 2: Training SOM Iteratively ==========================
for t = 1:Iteration
% Size of topological neighbourhood of the winning neuron at the
% iteration of i
width = width_Initial*exp(-t/t_width);
width_Variance = width^2;
% The time-varying learning rate at the iteration of i
learningRate = learningRate_Initial*exp(-t/t_learningRate);
% Prevent learning rate become too small
if learningRate <0.025
learningRate = 0.1;
end
%% ================= The Competitive Process =========================
% Compute the Euclidean distance between each neuron and input
[euclideanDist, index] = findBestMatch( train_data, somMap, somRow, ...
somCol, dataRow, dataCol );
% Find the index of winning neuron
[minM,ind] = min(euclideanDist(:));
[win_Row,win_Col] = ind2sub(size(euclideanDist),ind);
%% Return the index of winning neuron
%% ================= The End of Competitive Process ==================
%% ================= The Cooperative Process =========================
% Compute the neighborhood function for each neuron
neighborhood = computeNeighbourhood( somRow, somCol, win_Row, ...
win_Col, width_Variance);
%% Return the lateral distance between each neuron and winning neuron
%% ================= The End Of Cooperative Process ==================
%% ================= The Adaptive Process ============================
% Update the weight of all the neuron on the grid
somMap = updateWeight( train_data, somMap, somRow, somCol, ...
dataCol, index, learningRate, neighborhood)
%% ================= The End Of Adaptive Process =====================
%% ========== Illustrate The Updated Clustering Results ==============
% Weight vector of neuron
dot = zeros(somRow*somCol, dataCol);
% Matrix for SOM plot grid
matrix = zeros(somRow*somCol,1);
% Matrix for SOM plot grid for deletion
matrix_old = zeros(somRow*somCol,1);
ind = 1;
hold on;
f1 = figure(1);
set(f1,'name',strcat('Iteration #',num2str(t)),'numbertitle','off');
% Retrieve the weight vector of neuron
for r = 1:somRow
for c = 1:somCol
dot(ind,:)=reshape(somMap(r,c,:),1,dataCol);
ind = ind + 1;
end
end
% Plot SOM
for r = 1:somRow
Row_1 = 1+somRow*(r-1);
Row_2 = r*somRow;
Col_1 = somRow*somCol;
matrix(2*r-1,1) = plot(dot(Row_1:Row_2,1),dot(Row_1:Row_2,2),'--ro','LineWidth',2,'MarkerEdgeColor','g','MarkerFaceColor','g','MarkerSize',4);
matrix(2*r,1) = plot(dot(r:somCol:Col_1,1),dot(r:somCol:Col_1,2),'--ro','LineWidth',2,'MarkerEdgeColor','g','MarkerFaceColor','g','MarkerSize',4);
matrix_old(2*r-1,1) = matrix(2*r-1,1);
matrix_old(2*r,1) = matrix(2*r,1);
end
% Delete the SOM plot from previous iteration
if t~=Iteration
for r = 1:somRow
delete(matrix_old(2*r-1,1));
delete(matrix_old(2*r,1));
drawnow;
end
end
%% =================== The End Of Illustration =======================
end