Skip to content

Instantly share code, notes, and snippets.

@auroranockert
Created March 18, 2010 02:26
Show Gist options
  • Save auroranockert/335981 to your computer and use it in GitHub Desktop.
Save auroranockert/335981 to your computer and use it in GitHub Desktop.
% syn_sofm
%
% This function implements a Kohonen SOFM for clustering of a synthetic data
% set of 6 Gaussien distributions.
%
% March 2010, Mattias Ohlsson
% Email: mattias@thep.lu.se
% Clear
clear all;
close all;
% Default parameters
def_size = [5 5];
def_ndata = 100;
def_epoch = 100;
% Load the synthetic cluster data
ndata = input(sprintf('How many data? [%d] ', def_ndata));
if ndata > 0
[P,T] = loadclust1(ndata);
else
[P,T] = loadfacesORL(0.5);
end
% Initialize the random number generator
% tmp = input('Seed to the random number generator (Default current time): ');
% if tmp > 0
% rand('state',tmp);
% randn('state',tmp);
% else
% rand('state',sum(100*clock));
% randn('state',sum(100*clock));
% end
% Give the size of the output grid
tmp = input(sprintf('Give the size of the output grid [%d %d] ', ...
def_size(1), def_size(2)));
if tmp > 0
gsize = tmp;
else
gsize = def_size;
end
% Create the SOFM network
net = newsom(P,gsize,'gridtop','dist',0.9,1000,0.02);
% Hom many epochs
tmp = input(sprintf('Number of epochs? [%d] ', def_epoch));
if tmp > 0
epoch = tmp;
else
epoch = def_epoch;
end
noepoch = 0;
if ndata > 0
%% PLOTTING PART
% Plot of initial setup
w = net.iw{1,1};
plot(P(1,:),P(2,:),'.g','markersize',10)
hold on;
plotsom(net.iw{1,1},net.layers{1}.distances);
text = sprintf('Self Organizing Feature Map %dx%d nodex', gsize(1),gsize(2));
title(text);
drawnow;
hold off;
%% END OF PLOTTING PART
end
% Start to train the network
net.trainParam.epochs = epoch;
net = train(net,P);
% Some statistics
Y = sim(net,P);
Yc = vec2ind(Y);
for i=1:gsize(2)
for j=1:gsize(1)
nowins(i,j) = length(find(Yc == (i-1)*gsize(1)+j));
end
end
if ndata > 0
%% PLOTTING PART
plot(P(1,:),P(2,:),'.g','markersize',10)
hold on;
plotsom(net.iw{1,1},net.layers{1}.distances);
text = sprintf('Result after %d epochs', epoch);
title(text);
drawnow;
hold off;
%% END OF PLOTTING PART
else
for i = 1:gsize(2)
for j = 1:gsize(1)
showfacecluster(net, 'sofm', P, T, nowins(i, j));
end
end
end
disp('Matrix of the number of "wins" for the output nodes');
disp(nowins);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment