annotate toolboxes/FullBNT-1.0.7/netlab3.3/somtrain.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function net = somtrain(net, options, x)
Daniel@0 2 %SOMTRAIN Kohonen training algorithm for SOM.
Daniel@0 3 %
Daniel@0 4 % Description
Daniel@0 5 % NET = SOMTRAIN{NET, OPTIONS, X) uses Kohonen's algorithm to train a
Daniel@0 6 % SOM. Both on-line and batch algorithms are implemented. The learning
Daniel@0 7 % rate (for on-line) and neighbourhood size decay linearly. There is no
Daniel@0 8 % error function minimised during training (so there is no termination
Daniel@0 9 % criterion other than the number of epochs), but the sum-of-squares
Daniel@0 10 % is computed and returned in OPTIONS(8).
Daniel@0 11 %
Daniel@0 12 % The optional parameters have the following interpretations.
Daniel@0 13 %
Daniel@0 14 % OPTIONS(1) is set to 1 to display error values; also logs learning
Daniel@0 15 % rate ALPHA and neighbourhood size NSIZE. Otherwise nothing is
Daniel@0 16 % displayed.
Daniel@0 17 %
Daniel@0 18 % OPTIONS(5) determines whether the patterns are sampled randomly with
Daniel@0 19 % replacement. If it is 0 (the default), then patterns are sampled in
Daniel@0 20 % order. This is only relevant to the on-line algorithm.
Daniel@0 21 %
Daniel@0 22 % OPTIONS(6) determines if the on-line or batch algorithm is used. If
Daniel@0 23 % it is 1 then the batch algorithm is used. If it is 0 (the default)
Daniel@0 24 % then the on-line algorithm is used.
Daniel@0 25 %
Daniel@0 26 % OPTIONS(14) is the maximum number of iterations (passes through the
Daniel@0 27 % complete pattern set); default 100.
Daniel@0 28 %
Daniel@0 29 % OPTIONS(15) is the final neighbourhood size; default value is the
Daniel@0 30 % same as the initial neighbourhood size.
Daniel@0 31 %
Daniel@0 32 % OPTIONS(16) is the final learning rate; default value is the same as
Daniel@0 33 % the initial learning rate.
Daniel@0 34 %
Daniel@0 35 % OPTIONS(17) is the initial neighbourhood size; default 0.5*maximum
Daniel@0 36 % map size.
Daniel@0 37 %
Daniel@0 38 % OPTIONS(18) is the initial learning rate; default 0.9. This
Daniel@0 39 % parameter must be positive.
Daniel@0 40 %
Daniel@0 41 % See also
Daniel@0 42 % KMEANS, SOM, SOMFWD
Daniel@0 43 %
Daniel@0 44
Daniel@0 45 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 46
Daniel@0 47 % Check arguments for consistency
Daniel@0 48 errstring = consist(net, 'som', x);
Daniel@0 49 if ~isempty(errstring)
Daniel@0 50 error(errstring);
Daniel@0 51 end
Daniel@0 52
Daniel@0 53 % Set number of iterations in convergence phase
Daniel@0 54 if (~options(14))
Daniel@0 55 options(14) = 100;
Daniel@0 56 end
Daniel@0 57 niters = options(14);
Daniel@0 58
Daniel@0 59 % Learning rate must be positive
Daniel@0 60 if (options(18) > 0)
Daniel@0 61 alpha_first = options(18);
Daniel@0 62 else
Daniel@0 63 alpha_first = 0.9;
Daniel@0 64 end
Daniel@0 65 % Final learning rate must be no greater than initial learning rate
Daniel@0 66 if (options(16) > alpha_first | options(16) < 0)
Daniel@0 67 alpha_last = alpha_first;
Daniel@0 68 else
Daniel@0 69 alpha_last = options(16);
Daniel@0 70 end
Daniel@0 71
Daniel@0 72 % Neighbourhood size
Daniel@0 73 if (options(17) >= 0)
Daniel@0 74 nsize_first = options(17);
Daniel@0 75 else
Daniel@0 76 nsize_first = max(net.map_dim)/2;
Daniel@0 77 end
Daniel@0 78 % Final neighbourhood size must be no greater than initial size
Daniel@0 79 if (options(15) > nsize_first | options(15) < 0)
Daniel@0 80 nsize_last = nsize_first;
Daniel@0 81 else
Daniel@0 82 nsize_last = options(15);
Daniel@0 83 end
Daniel@0 84
Daniel@0 85 ndata = size(x, 1);
Daniel@0 86
Daniel@0 87 if options(6)
Daniel@0 88 % Batch algorithm
Daniel@0 89 H = zeros(ndata, net.num_nodes);
Daniel@0 90 end
Daniel@0 91 % Put weights into matrix form
Daniel@0 92 tempw = sompak(net);
Daniel@0 93
Daniel@0 94 % Then carry out training
Daniel@0 95 j = 1;
Daniel@0 96 while j <= niters
Daniel@0 97 if options(6)
Daniel@0 98 % Batch version of algorithm
Daniel@0 99 alpha = 0.0;
Daniel@0 100 frac_done = (niters - j)/niters;
Daniel@0 101 % Compute neighbourhood
Daniel@0 102 nsize = round((nsize_first - nsize_last)*frac_done + nsize_last);
Daniel@0 103
Daniel@0 104 % Find winning node: put weights back into net so that we can
Daniel@0 105 % call somunpak
Daniel@0 106 net = somunpak(net, tempw);
Daniel@0 107 [temp, bnode] = somfwd(net, x);
Daniel@0 108 for k = 1:ndata
Daniel@0 109 H(k, :) = reshape(net.inode_dist(:, :, bnode(k))<=nsize, ...
Daniel@0 110 1, net.num_nodes);
Daniel@0 111 end
Daniel@0 112 s = sum(H, 1);
Daniel@0 113 for k = 1:net.num_nodes
Daniel@0 114 if s(k) > 0
Daniel@0 115 tempw(k, :) = sum((H(:, k)*ones(1, net.nin)).*x, 1)/ ...
Daniel@0 116 s(k);
Daniel@0 117 end
Daniel@0 118 end
Daniel@0 119 else
Daniel@0 120 % On-line version of algorithm
Daniel@0 121 if options(5)
Daniel@0 122 % Randomise order of pattern presentation: with replacement
Daniel@0 123 pnum = ceil(rand(ndata, 1).*ndata);
Daniel@0 124 else
Daniel@0 125 pnum = 1:ndata;
Daniel@0 126 end
Daniel@0 127 % Cycle through dataset
Daniel@0 128 for k = 1:ndata
Daniel@0 129 % Fraction done
Daniel@0 130 frac_done = (((niters+1)*ndata)-(j*ndata + k))/((niters+1)*ndata);
Daniel@0 131 % Compute learning rate
Daniel@0 132 alpha = (alpha_first - alpha_last)*frac_done + alpha_last;
Daniel@0 133 % Compute neighbourhood
Daniel@0 134 nsize = round((nsize_first - nsize_last)*frac_done + nsize_last);
Daniel@0 135 % Find best node
Daniel@0 136 pat_diff = ones(net.num_nodes, 1)*x(pnum(k), :) - tempw;
Daniel@0 137 [temp, bnode] = min(sum(abs(pat_diff), 2));
Daniel@0 138
Daniel@0 139 % Now update neighbourhood
Daniel@0 140 neighbourhood = (net.inode_dist(:, :, bnode) <= nsize);
Daniel@0 141 tempw = tempw + ...
Daniel@0 142 ((alpha*(neighbourhood(:)))*ones(1, net.nin)).*pat_diff;
Daniel@0 143 end
Daniel@0 144 end
Daniel@0 145 if options(1)
Daniel@0 146 % Print iteration information
Daniel@0 147 fprintf(1, 'Iteration %d; alpha = %f, nsize = %f. ', j, alpha, ...
Daniel@0 148 nsize);
Daniel@0 149 % Print sum squared error to nearest node
Daniel@0 150 d2 = dist2(tempw, x);
Daniel@0 151 fprintf(1, 'Error = %f\n', sum(min(d2)));
Daniel@0 152 end
Daniel@0 153 j = j + 1;
Daniel@0 154 end
Daniel@0 155
Daniel@0 156 net = somunpak(net, tempw);
Daniel@0 157 options(8) = sum(min(dist2(tempw, x)));