Skip to content

Commit ebc7f31

Browse files
authored
Add files via upload
1 parent 57b79f7 commit ebc7f31

24 files changed

+876
-0
lines changed

dbfun.m

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function DB=dbfun(data,net)
2+
% Calculate Davies–Bouldin index
3+
4+
% Directed Batch Growing Self Organizing Map (DBGSOM)
5+
% version 1.0 - July 2017
6+
% Mahdi Vasighi
7+
% Institute for Advanced Studies in Basic Sciences, Zanjan, Iran
8+
% Department of Computer Science and Information Technology
9+
% www.iasbs.ac.ir/~vasighi/
10+
11+
W=net.W;
12+
hitcount=net.hitcount;
13+
winlist=net.winlist;
14+
15+
[data,~]=prefun(data,'rs');
16+
17+
for i=1:size(W,2)
18+
for j=i+1:size(W,2)
19+
if hitcount(j)~=0 && hitcount(i)~=0;
20+
di=mean(dist(data(winlist==i,:),W(:,i)));
21+
dj=mean(dist(data(winlist==j,:),W(:,j)));
22+
dij=dist(W(:,i)',W(:,j));
23+
D2(i,j)=(di+dj)/dij;
24+
D2(j,i)=(di+dj)/dij;
25+
end
26+
end
27+
end
28+
DB=sum(max(D2))/sum(hitcount~=0);

dbgrowfun.m

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
function net=dbgrowfun(net,netset,ds,bo_id)
2+
% Growing phase function in batch mode learning
3+
4+
% Directed Batch Growing Self Organizing Map (DBGSOM)
5+
% version 1.0 - July 2017
6+
% Mahdi Vasighi
7+
% Institute for Advanced Studies in Basic Sciences, Zanjan, Iran
8+
% Department of Computer Science and Information Technology
9+
% www.iasbs.ac.ir/~vasighi/
10+
11+
W=net.W;
12+
Err=net.Err;
13+
grd=net.grd;
14+
15+
16+
[sorted_Err,x1id]=sort(Err(bo_id),'descend');
17+
bo_id=bo_id(x1id);
18+
bo_id=bo_id(Err(bo_id)>=netset.GT); % Run growing phase only for b-node (Err>=GT)
19+
20+
%Growing phase
21+
for i=1:length(bo_id)
22+
new_grd=[];
23+
W_new_grd=[];
24+
new_Err=[];
25+
26+
notallowed_pos=[ismember([grd(:,bo_id(i))+[0;1]]',grd','rows') ismember([grd(:,bo_id(i))+[0;-1]]',grd','rows'),...
27+
[ismember([grd(:,bo_id(i))+[1;0]]',grd','rows') ismember([grd(:,bo_id(i))+[-1;0]]',grd','rows')]];
28+
29+
direct_opr=[0 0 1 -1;1 -1 0 0];%[N S E W] directions
30+
direct_opr(:,notallowed_pos)=[];
31+
allowed_grd=repmat(grd(:,bo_id(i)),1,size(direct_opr,2))+direct_opr;
32+
33+
34+
if ~isempty(allowed_grd)
35+
36+
nbr1_id=find(ds(bo_id(i),:)==1);
37+
nbr2_id=find(ds(bo_id(i),:)==2);
38+
39+
%sorting neighbirs of boundary nodes
40+
[sorted_Err1,x1id]=sort(Err(nbr1_id),'descend');%
41+
nbr1_id=nbr1_id(x1id);
42+
[sorted_Err2,x2id]=sort(Err(nbr2_id),'descend');%
43+
nbr2_id=nbr2_id(x2id);
44+
45+
46+
if size(allowed_grd,2)==3 && length(nbr1_id)==1 %three possible postion to grow
47+
sel_nbr2_id = nbr2_id(min(dist(allowed_grd',grd(:,nbr2_id)))==1);
48+
nbr1_dist = dist(grd(:,nbr1_id)',allowed_grd);
49+
new_Err=0;
50+
if isempty(sel_nbr2_id) % if there is no allowed nbr2
51+
temp_id=find(nbr1_dist~=2);
52+
new_grd = allowed_grd(:,temp_id(1));
53+
W_new_grd = 2.*W(:,bo_id(i))-W(:,nbr1_id);
54+
55+
else % there is nbr2
56+
[mval,max_id]=max(Err(sel_nbr2_id));
57+
nbr2_dist = dist(allowed_grd',grd(:,sel_nbr2_id(max_id)));
58+
new_grd = allowed_grd(:,nbr2_dist==1);
59+
60+
W_new_grd = (W(:,bo_id(i))+W(:,sel_nbr2_id(max_id)))/2;
61+
end
62+
63+
64+
elseif size(allowed_grd,2)==2 && length(nbr1_id)==2%two possible positions to grow
65+
[~,max_id]=max(Err(nbr1_id));
66+
[~,min_id]=min(Err(nbr1_id));
67+
[~,ind]=min(dist(grd(:,nbr1_id(max_id))',allowed_grd));
68+
69+
sel_nbr2_id = nbr2_id(min(dist(allowed_grd',grd(:,nbr2_id)))==1);
70+
[~,max2_id]=max(Err(sel_nbr2_id));
71+
72+
new_grd=allowed_grd(:,ind(1));
73+
new_Err=0;
74+
75+
if isempty(sel_nbr2_id) % if there is no adj nbr2
76+
W_new_grd = 2.*W(:,bo_id(i))-W(:,nbr1_id(min_id)); % can be change (mean with bo)
77+
else
78+
if dist(grd(nbr1_id(max_id))',grd(:,sel_nbr2_id(max2_id)))==1 % nbr2 is adj to selected allowed grd
79+
W_new_grd = (2.*W(:,bo_id(i))-W(:,nbr1_id(min_id))+W(:,sel_nbr2_id(max2_id)))/2;
80+
else
81+
W_new_grd = 2.*W(:,bo_id(i))-W(:,nbr1_id(min_id));
82+
end
83+
end
84+
85+
86+
87+
else
88+
if length(nbr1_id)==3 %one possible position to grow
89+
new_grd=allowed_grd;
90+
new_Err=0;
91+
nbr1_dist = dist(allowed_grd',grd(:,nbr1_id));
92+
sel_nbr2_id = nbr2_id(min(dist(allowed_grd',grd(:,nbr2_id)))==1);
93+
94+
if isempty(sel_nbr2_id) % if there is no adj nbr2
95+
W_new_grd = 2.*W(:,bo_id(i))-W(:,nbr1_id(nbr1_dist==2));
96+
else
97+
[~,max2_id]=max(Err(sel_nbr2_id));
98+
W_new_grd = (2.*W(:,bo_id(i))-W(:,nbr1_id(nbr1_dist==2))+W(:,sel_nbr2_id(max2_id)))/2;
99+
end
100+
101+
end
102+
end
103+
grd=[grd,new_grd];
104+
W=[W,W_new_grd];
105+
Err=[Err,new_Err];
106+
107+
end
108+
end
109+
net.grd=grd;
110+
net.W=W;

dbgsom.m

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
function net=dbgsom(data,netset)
2+
3+
% Training function for DBGSOM
4+
5+
% Directed Batch Growing Self Organizing Map (DBGSOM)
6+
% version 1.0 - July 2017
7+
% Mahdi Vasighi
8+
% Institute for Advanced Studies in Basic Sciences, Zanjan, Iran
9+
% Department of Computer Science and Information Technology
10+
% www.iasbs.ac.ir/~vasighi/
11+
12+
[n1,m1]=size(data);
13+
[data,pre_param]=prefun(data,'rs'); %data pre-processing
14+
data_tmp=data;
15+
16+
net=dbgsom_init(data); %gsom initialization
17+
18+
ds=linkdist(net.grd);
19+
20+
epvec=1:netset.epch;
21+
22+
netset.pvec=(netset.pmax-netset.pmin)*((netset.epch-epvec)/(netset.epch-1))+netset.pmin;
23+
24+
netset.lrvec=(netset.amax-netset.amin)*((netset.epch-epvec)/(netset.epch-1))+netset.amin; % Learning rate
25+
netset.GT=-m1*log(netset.sf);
26+
27+
fprintf('Data matrix (Samples=%i Variables=%i)\n',n1,m1)
28+
fprintf('DBGSOM (SF=%3.2f)\n',netset.sf)
29+
30+
for ep=1:netset.epch
31+
if ~rem(ep,10)
32+
fprintf('Training epoch %i\n',ep)
33+
end
34+
35+
h=nbrfun(netset,ds,ep);
36+
[~,net.win_list]=winfun(data,net.W);
37+
38+
hh=h(:,net.win_list);
39+
h1=data'*hh';
40+
h2= true(size(data'))*hh';
41+
pos = find(h2 > 0);
42+
net.W(pos)=h1(pos)./h2(pos);
43+
44+
if ep<netset.epch && ep>1
45+
46+
%calulcating error of each neuron
47+
[win_err,win_id]=winfun(data,net.W);
48+
net.Err=errcalc(win_err,win_id,net.grd);
49+
50+
% check to find boundary and non-boundary nodes.
51+
[nb_id,bo_id]=findbound(ds);
52+
53+
% distibute the error of non-boundary nodes which has a higher error
54+
% than GT (batch mode)
55+
for jj=1:length(nb_id)
56+
if net.Err(nb_id(jj))>=netset.GT
57+
intersect_id=intersect(find(ds(nb_id(jj),:)==1),bo_id);
58+
if ~isempty(intersect_id)
59+
net.Err(intersect_id)=net.Err(intersect_id)+net.Err(nb_id(jj))/8; % error of a nb node is distributed equally between neighbors and itself
60+
net.Err(nb_id(jj))=net.Err(nb_id(jj))/2;
61+
end
62+
end
63+
end
64+
65+
net=dbgrowfun(net,netset,ds,bo_id);
66+
67+
ds=linkdist(net.grd);
68+
69+
end
70+
% ploting neurons in data sapce
71+
if strcmp(netset.vis,'y')
72+
vistrn(net,data)
73+
end
74+
75+
end
76+
fprintf('[Done]\n')
77+
78+
[~,winlist]=winfun(data_tmp,net.W);
79+
[hitcount,~]=hist(winlist,size(net.W,2));
80+
net.winlist=winlist;
81+
net.hitcount=hitcount;
82+
net.pre_param=pre_param;
83+
net=net_eval(data,net);
84+

dbgsom_3rd.wmap_iris.tif

32.1 KB
Binary file not shown.

dbgsom_demo.m

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
% Directed Batch Growing Self Organizing Map (DBGSOM)
2+
% version 1.0 - July 2017
3+
% Mahdi Vasighi
4+
% Institute for Advanced Studies in Basic Sciences, Zanjan, Iran
5+
% Department of Computer Science and Information Technology
6+
% www.iasbs.ac.ir/~vasighi/
7+
8+
% Demo: Iris data analysis
9+
10+
% Directed Batch Growing Self Organizing Map (DBGSOM), version 1.0
11+
% Copyright (C) 2017 Mahdi Vasighi
12+
%
13+
% This program is free software: you can redistribute it and/or modify
14+
% it under the terms of the GNU General Public License as published by
15+
% the Free Software Foundation, either version 3 of the License, or
16+
% any later version.
17+
%
18+
% This program is distributed in the hope that it will be useful,
19+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
% GNU General Public License for more details.
22+
%
23+
% You should have received a copy of the GNU General Public License
24+
% along with this program. If not, see <http://www.gnu.org/licenses/>.
25+
26+
close all
27+
clear
28+
clc
29+
30+
load iris % load data matrix (samples x variables)
31+
32+
netset=setting('dbgsom'); % initialize setting for dbgsom
33+
34+
netset.epch = 100; % number of epochs
35+
netset.sf = 0.7; % Spread Factor setting
36+
netset.vis ='n';
37+
38+
net=dbgsom(X,netset);
39+
40+
figure(1);vismap(net,class) %Top-map visulaization
41+
figure(2);vishit(net) %Hit-map visualization

dbgsom_hitmap_iris.tif

77.4 KB
Binary file not shown.

dbgsom_init.m

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function net=dbgsom_init(data)
2+
3+
% DBGSOM initializatio
4+
5+
% Directed Batch Growing Self Organizing Map (DBGSOM)
6+
% version 1.0 - July 2017
7+
% Mahdi Vasighi
8+
% Institute for Advanced Studies in Basic Sciences, Zanjan, Iran
9+
% Department of Computer Science and Information Technology
10+
% www.iasbs.ac.ir/~vasighi/
11+
12+
% Directed Batch Growing Self Organizing Map (DBGSOM), version 1.0
13+
% Copyright (C) 2017 Mahdi Vasighi
14+
%
15+
% This program is free software: you can redistribute it and/or modify
16+
% it under the terms of the GNU General Public License as published by
17+
% the Free Software Foundation, either version 3 of the License, or
18+
% any later version.
19+
%
20+
% This program is distributed in the hope that it will be useful,
21+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23+
% GNU General Public License for more details.
24+
%
25+
% You should have received a copy of the GNU General Public License
26+
% along with this program. If not, see <http://www.gnu.org/licenses/>.
27+
28+
grd=[0 0;1 0;0 1;1 1]'; %grid positions
29+
W=mean(data)'*ones(1,4)+0.1*randn(size(data,2),size(grd,2));
30+
% W=rand(size(X,2),size(grd,2));
31+
net.W=W;
32+
net.grd=grd;

dbgsom_topmap_iris.tif

40.2 KB
Binary file not shown.

dmfun.m

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function DM=dmfun(data,net)
2+
% Calculate distorsion measure
3+
4+
% Directed Batch Growing Self Organizing Map (DBGSOM)
5+
% version 1.0 - July 2017
6+
% Mahdi Vasighi
7+
% Institute for Advanced Studies in Basic Sciences, Zanjan, Iran
8+
% Department of Computer Science and Information Technology
9+
% www.iasbs.ac.ir/~vasighi/
10+
11+
% Directed Batch Growing Self Organizing Map (DBGSOM), version 1.0
12+
% Copyright (C) 2017 Mahdi Vasighi
13+
%
14+
% This program is free software: you can redistribute it and/or modify
15+
% it under the terms of the GNU General Public License as published by
16+
% the Free Software Foundation, either version 3 of the License, or
17+
% any later version.
18+
%
19+
% This program is distributed in the hope that it will be useful,
20+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
% GNU General Public License for more details.
23+
%
24+
% You should have received a copy of the GNU General Public License
25+
% along with this program. If not, see <http://www.gnu.org/licenses/>.
26+
27+
W=net.W;
28+
winlist=net.winlist;
29+
hitcount=net.hitcount;
30+
31+
[data,~]=prefun(data,'rs');
32+
33+
ntds=linkdist(net.grd);
34+
h = exp(-(ntds.^2)/(2*((1)^2)));
35+
hh=h(:,winlist);
36+
dst=dist(data,W);
37+
38+
hitind=hitcount>0;
39+
dm=hh.*(dst.^2)';
40+
dm=dm(hitind,:);
41+
DM=mean(sum(dm./repmat(hitcount(hitind)',1,size(data,1))));

errcalc.m

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function Err=errcalc(win_err,win_id,grd)
2+
% Accumulative error calculation
3+
4+
% Directed Batch Growing Self Organizing Map (DBGSOM)
5+
% version 1.0 - July 2017
6+
% Mahdi Vasighi
7+
% Institute for Advanced Studies in Basic Sciences, Zanjan, Iran
8+
% Department of Computer Science and Information Technology
9+
% www.iasbs.ac.ir/~vasighi/
10+
11+
% Directed Batch Growing Self Organizing Map (DBGSOM), version 1.0
12+
% Copyright (C) 2017 Mahdi Vasighi
13+
%
14+
% This program is free software: you can redistribute it and/or modify
15+
% it under the terms of the GNU General Public License as published by
16+
% the Free Software Foundation, either version 3 of the License, or
17+
% any later version.
18+
%
19+
% This program is distributed in the hope that it will be useful,
20+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
% GNU General Public License for more details.
23+
%
24+
% You should have received a copy of the GNU General Public License
25+
% along with this program. If not, see <http://www.gnu.org/licenses/>.
26+
27+
Err=zeros(1,size(grd,2));
28+
for i=1:size(grd,2)
29+
Err(i)=sum(win_err(find(win_id==i)));
30+
end

0 commit comments

Comments
 (0)