wolffd@0: function net = som(nin, map_size) wolffd@0: %SOM Creates a Self-Organising Map. wolffd@0: % wolffd@0: % Description wolffd@0: % NET = SOM(NIN, MAP_SIZE) creates a SOM NET with input dimension (i.e. wolffd@0: % data dimension) NIN and map dimensions MAP_SIZE. Only two- wolffd@0: % dimensional maps are currently implemented. wolffd@0: % wolffd@0: % The fields in NET are wolffd@0: % type = 'som' wolffd@0: % nin = number of inputs wolffd@0: % map_dim = dimension of map (constrained to be 2) wolffd@0: % map_size = grid size: number of nodes in each dimension wolffd@0: % num_nodes = number of nodes: the product of values in map_size wolffd@0: % map = map_dim+1 dimensional array containing nodes wolffd@0: % inode_dist = map of inter-node distances using Manhatten metric wolffd@0: % wolffd@0: % The map contains the node vectors arranged column-wise in the first wolffd@0: % dimension of the array. wolffd@0: % wolffd@0: % See also wolffd@0: % KMEANS, SOMFWD, SOMTRAIN wolffd@0: % wolffd@0: wolffd@0: % Copyright (c) Ian T Nabney (1996-2001) wolffd@0: wolffd@0: net.type = 'som'; wolffd@0: net.nin = nin; wolffd@0: wolffd@0: % Create Map of nodes wolffd@0: if round(map_size) ~= map_size | (map_size < 1) wolffd@0: error('SOM specification must contain positive integers'); wolffd@0: end wolffd@0: wolffd@0: net.map_dim = length(map_size); wolffd@0: if net.map_dim ~= 2 wolffd@0: error('SOM is a 2 dimensional map'); wolffd@0: end wolffd@0: net.num_nodes = prod(map_size); wolffd@0: % Centres are stored by column as first index of multi-dimensional array. wolffd@0: % This makes extracting them later more easy. wolffd@0: % Initialise with rand to create square grid wolffd@0: net.map = rand([nin, map_size]); wolffd@0: net.map_size = map_size; wolffd@0: wolffd@0: % Crude function to compute inter-node distances wolffd@0: net.inode_dist = zeros([map_size, net.num_nodes]); wolffd@0: for m = 1:net.num_nodes wolffd@0: node_loc = [1+fix((m-1)/map_size(2)), 1+rem((m-1),map_size(2))]; wolffd@0: for k = 1:map_size(1) wolffd@0: for l = 1:map_size(2) wolffd@0: net.inode_dist(k, l, m) = round(max(abs([k l] - node_loc))); wolffd@0: end wolffd@0: end wolffd@0: end