📄 som_net.m
字号:
function net = som_net(X,settings,class);
% som_net calculates the Kohonen map (Self Organizing Map) weights
% and the output weights (Counter Propagation ANN) if class is defined
%
% see the HTML HELP files (help.htm) for details
% or type "help model_kohonen" or "help model_cpann"
%
% net = som_model(X,settings,class)
%
% input:
% X data [n x p]
% settings setting structure
%
% optional input, only for CPANNs:
% class class vector [n x 1]
%
% output:
% net structure containing all the map information (used settings
% and calculated weights [size x size x p]
%
% see the HTML HELP files (help.htm) for details
nsize = settings.nsize;
enter = settings.enter;
nobj = size(X,1);
nvar = size(X,2);
display_mod = 10; % display epochs every display_mod
start_epoch = 1;
end_epoch = settings.epochs;
if nargin == 3
net_type = 'cpann';
else
net_type = 'kohonen';
end
% initializes kohonen weights inbetween 0.1 and 0.9
W = rand(nsize, nsize, size(X,2)).*0.8 + 0.1;
W = reshape(permute(W,[2 1 3]),[nsize*nsize nvar]);
% initialization for weight residuals
% Wold = W;
if strcmpi(net_type,'cpann')
in_out = zeros(size(class,1),max(class));
for i=1:size(class,1); in_out(i,class(i)) = 1; end;
W_out = ones(nsize, nsize, size(in_out,2)).*(1/max(class));
W_out = reshape(permute(W_out,[2 1 3]),[nsize*nsize size(in_out,2)]);
end
disp('training net...')
in_order = 1:nobj;
for e = start_epoch:end_epoch
if mod(e,display_mod) == 0
disp(['training epochs: ' num2str(e) ' of ' num2str(end_epoch)])
end
if strcmpi(enter,'random')
in_order = randperm(nobj);
end
for i = 1:nobj
x_in = X(in_order(i),:);
% calculates winning neuron
winner = som_winner(x_in,W);
% applies corrections
[Wnew,param_correction] = som_update(x_in,W,winner,settings,e,i,nobj);
W = Wnew;
if strcmpi(net_type,'cpann')
class_in = in_out(in_order(i),:);
W_out_new = cpann_update(class_in,W_out,winner,settings,e,i,nobj,param_correction);
W_out = W_out_new;
end
end
% calculation of weight residuals
%if mod(e,display_mod) == 0
% W_res = (W - Wold).^2;
% res(e/display_mod,1) = e;
% res(e/display_mod,2) = sum(sum(W_res));
% Wold = W;
%end
end
disp('...training finished')
% readapts W from reshaping
for k=1:nsize
for j=1:nsize
ind = som_which_col(k,j,size(W,1));
W_final(k,j,:) = W(ind,:);
end
end
% saves results
net.W = W_final;
if strcmpi(net_type,'cpann')
for k=1:nsize
for j=1:nsize
ind = som_which_col(k,j,size(W_out,1));
% normilizes output weights (sum up to 1)
W_out_final(k,j,:) = W_out(ind,:)/sum(W_out(ind,:));
end
end
net.W_out = W_out_final;
end
net.settings = settings;
% showing residual plot
%figure
%hold on
%plot(res(:,1),res(:,2),'k');
%plot(res(:,1),res(:,2),'r*');
%xlabel('epochs');
%ylabel('squared residuals');
%title(['epochs ' num2str(end_epoch)])
%hold off
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -