-
Notifications
You must be signed in to change notification settings - Fork 0
/
Iris_SOM_Train.m
78 lines (58 loc) · 1.74 KB
/
Iris_SOM_Train.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
clear;
close all;
%% Data Input
load('IrisData.mat')
N_DATA = 150;
feature = IrisData(1:N_DATA,1:4);
feature = feature{:,:};
[feature_row,feature_col] = size(feature);
%% Normalisasi
%Normalisasi Min-Max
feature_norm = zeros(size(feature));
for m = 1 : feature_row
for n_train = 1 : feature_col
feature_norm(m,n_train) = ((feature(m,n_train) - min(feature(:,n_train)))/(max(feature(:,n_train)) - min(feature(:,n_train))));
end
end
%% Hyperparameter & Declare Variable
%Hyperparameter
x_size = feature_col; %Input layer
y_size = 2; %Output layer (number of cluster(s))
alpha = 0.6;
beta = 0.99; %alpha decrease rate
max_epoch = 1000;
%Variable
stop = 0;
alpha_target = 0.00001;
epoch_count = 1;
%% Initialisasi
%Random init
rng(0) %Randomizer seed
epsilon_init = 1; %Range random number
%Weight init
weight = rand(x_size, y_size) * 2 * epsilon_init - epsilon_init;
disp(weight) % Weight before training
%% Training
while stop == 0 && epoch_count <= max_epoch
for n_train = 1:N_DATA
% Euclidian distance calculation
for l = 1:y_size
i_vect = feature_norm(n_train,:);
c_vect = weight(:,l);
e_dist(l) = sqrt(sum((i_vect - transpose(c_vect)) .^2));
end
% Closest cluster determination
[c_dist, c_ind] = min(e_dist);
% Weight update on closest cluster
for i = 1:x_size
weight(i,c_ind) = weight(i,c_ind) + alpha*(i_vect(i)-weight(i,c_ind));
end
end
alpha = beta * alpha; % Learning rate update
if alpha <= alpha_target
stop = 1;
end
epoch_count = epoch_count + 1;
end
disp(weight) % Weight after training
Iris_SOM_Test