function kn_demo;
% KN_DEMO - demo of 6*7 Kohonen Network
% Each node has two weights. Net is supplied with random pairs
% The algorithm runs over 9 epochs, presenting the input many times
% After each epoch, the weight vectors are plotted - lines between adjacent nodes
% The net is seen to order the weights
% Dr Richard Mitchell, 11/8/03 ... 27/7/05
clf;
net = rand(6,7,2) % net is 6 rows 7 cols, 2 weights
ins = rand(200,2); % input 200 random pairs
neigh = [4 3 2 1 1 1 1 1]; % the neighbourhood at each epoch
% and the learning rate
lrate = [0.5 0.4 0.3 0.2 0.1 0.05 0.03 0.02];
for epoch = 1:length(neigh)
draw(net, epoch) % show current weights
% now present data 40 times ...
net = kn_learn(net, ins, neigh(epoch), lrate(epoch), 40);
end
draw(net, 9); % show final state of net
net
function draw(net, epoch)
% draw network at its current state
subplot(3,3,epoch); % set appropriate subplot
hold on % so can keep drawing lines
for r = 1:size(net, 1)-1 % for all rows, and columns
for c=1:size(net, 2)-1
% draw box between node and those in next row and column
plot([net(r,c,1), net(r+1,c,1), net(r+1,c+1,1), net(r,c+1,1), net(r,c,1)], ...
[net(r,c,2), net(r+1,c,2), net(r+1,c+1,2), net(r,c+1,2), net(r,c,2)],'k')
end
end
hold off
set(gca, 'xlim', [0 1], 'ylim', [0 1]); % ensure plot in range 0..1