Skip to content

Commit 9ac17c2

Browse files
committed
2 parents 4a5e728 + c7cea9d commit 9ac17c2

File tree

1 file changed

+80
-80
lines changed

1 file changed

+80
-80
lines changed

SOM/SOM_net.m

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,86 @@
22
%%
33
%clc; clear all; close all;
44
clear;clc;close all
5-
% tic;
6-
% %location
7-
%
8-
% path_train='train.txt';
9-
% train_label = repmat(1,1,676)
10-
% test_label = repmat(1,1,676)
11-
% train_data = load(path_train);
12-
% [data_row,data_clown]=size(train_data);
13-
%
14-
%
15-
% %s1 = struct('train_data_1',train_data_1,'train_label_1',train_label_1,'test_data_1',test_data_1,'test_label_1',test_label_1)
16-
%
17-
%
18-
%
19-
% %SOM network m*n
20-
% m=10;
21-
% n=10;
22-
% %Neuron som_sum
23-
% som_sum=m*n;
24-
% %weight initialize
25-
% w = rand(som_sum, data_clown);
26-
% %Initialize learning rate
27-
% learn0 = 0.6;
28-
% learn_rate = learn0;
29-
% %learning para
30-
% learn_para=100;
31-
% %Setting iteration
32-
% iter = 100;%1000 500
33-
% %Neuron location
34-
% [I,J] = ind2sub([m, n], 1:som_sum);
35-
% %Neighbor Initialize
36-
% neighbor0 =2;
37-
% neighbor_redius = neighbor0;
38-
% %Neighbor parameters
39-
% neighbor_para = 1000/log(neighbor0);
40-
%
41-
% for t=1:iter
42-
% % Scan all the sample points
43-
% display(['Iteration: ' num2str(t)]);
44-
% for j=1:data_row
45-
% %Getting the winner neuron
46-
% data_x = train_data(j,:);
47-
% %Find the winner neuron
48-
% [win_row, win_som_index]=min(dist(data_x,w'));
49-
% %Find the winner neuron position
50-
% [win_som_row,win_som_cloumn] = ind2sub([m, n],win_som_index);
51-
% win_som=[win_som_row,win_som_cloumn];
52-
% %Calculate and update the winner neuron neighbourhood position
53-
% %distance_som = sum(( ([I( : ), J( : )] - repmat(win_som, som_sum,1)) .^2) ,2);
54-
% distance_som = exp( sum(( ([I( : ), J( : )] - repmat(win_som, som_sum,1)) .^2) ,2)/(-2*neighbor_redius*neighbor_redius)) ;
55-
% %Update the weights
56-
% for i = 1:som_sum
57-
% % if distance_som(i)<neighbor_redius*neighbor_redius
58-
% w(i,:) = w(i,:) + learn_rate.*distance_som(i).*( data_x - w(i,:));
59-
% end
60-
% end
61-
%
62-
% %Updateing the learning rate
63-
% learn_rate = learn0 * exp(-t/learn_para);
64-
% %Update the neighbor radius
65-
% neighbor_redius = neighbor0*exp(-t/neighbor_para);
66-
% end
67-
%
68-
%
69-
%
70-
% %save as som_num
71-
% som_num=cell(1,size(w,1));
72-
% for i=1:size(w,1)
73-
% som_num{1,i}=[];
74-
% end
75-
% %Every neuron data corresponding number
76-
% for num=1:data_row
77-
% [som_row,clown]= min(sum(( (w - repmat(train_data(num,:), som_sum,1)) .^2) ,2));
78-
% som_num{1,clown}= [som_num{1,clown},num];
79-
% end
80-
%
81-
%
82-
% path1='som_num_1.mat';
83-
% save(path1,'som_num');
84-
% toc;
5+
tic;
6+
%location
7+
8+
path_train='train.txt';
9+
train_label = repmat(1,1,676)
10+
test_label = repmat(1,1,676)
11+
train_data = load(path_train);
12+
[data_row,data_clown]=size(train_data);
13+
14+
15+
%s1 = struct('train_data_1',train_data_1,'train_label_1',train_label_1,'test_data_1',test_data_1,'test_label_1',test_label_1)
16+
17+
18+
19+
%SOM network m*n
20+
m=10;
21+
n=10;
22+
%Neuron som_sum
23+
som_sum=m*n;
24+
%weight initialize
25+
w = rand(som_sum, data_clown);
26+
%Initialize learning rate
27+
learn0 = 0.6;
28+
learn_rate = learn0;
29+
%learning para
30+
learn_para=100;
31+
%Setting iteration
32+
iter = 100;%1000 500
33+
%Neuron location
34+
[I,J] = ind2sub([m, n], 1:som_sum);
35+
%Neighbor Initialize
36+
neighbor0 =2;
37+
neighbor_redius = neighbor0;
38+
%Neighbor parameters
39+
neighbor_para = 1000/log(neighbor0);
40+
41+
for t=1:iter
42+
% Scan all the sample points
43+
display(['Iteration: ' num2str(t)]);
44+
for j=1:data_row
45+
%Getting the winner neuron
46+
data_x = train_data(j,:);
47+
%Find the winner neuron
48+
[win_row, win_som_index]=min(dist(data_x,w'));
49+
%Find the winner neuron position
50+
[win_som_row,win_som_cloumn] = ind2sub([m, n],win_som_index);
51+
win_som=[win_som_row,win_som_cloumn];
52+
%Calculate and update the winner neuron neighbourhood position
53+
%distance_som = sum(( ([I( : ), J( : )] - repmat(win_som, som_sum,1)) .^2) ,2);
54+
distance_som = exp( sum(( ([I( : ), J( : )] - repmat(win_som, som_sum,1)) .^2) ,2)/(-2*neighbor_redius*neighbor_redius)) ;
55+
%Update the weights
56+
for i = 1:som_sum
57+
% if distance_som(i)<neighbor_redius*neighbor_redius
58+
w(i,:) = w(i,:) + learn_rate.*distance_som(i).*( data_x - w(i,:));
59+
end
60+
end
61+
62+
%Updateing the learning rate
63+
learn_rate = learn0 * exp(-t/learn_para);
64+
%Update the neighbor radius
65+
neighbor_redius = neighbor0*exp(-t/neighbor_para);
66+
end
67+
68+
69+
70+
%save as som_num
71+
som_num=cell(1,size(w,1));
72+
for i=1:size(w,1)
73+
som_num{1,i}=[];
74+
end
75+
%Every neuron data corresponding number
76+
for num=1:data_row
77+
[som_row,clown]= min(sum(( (w - repmat(train_data(num,:), som_sum,1)) .^2) ,2));
78+
som_num{1,clown}= [som_num{1,clown},num];
79+
end
80+
81+
82+
path1='som_num_1.mat';
83+
save(path1,'som_num');
84+
toc;
8585
%%
8686
number1 = load('p_dataset_26/Sample1/img002-00001.mat')
8787
number2 = load('p_dataset_26/Sample2/img003-00001.mat')

0 commit comments

Comments
 (0)