Daniel@0: function net = gtminit(net, options, data, samp_type, varargin) Daniel@0: %GTMINIT Initialise the weights and latent sample in a GTM. Daniel@0: % Daniel@0: % Description Daniel@0: % NET = GTMINIT(NET, OPTIONS, DATA, SAMPTYPE) takes a GTM NET and Daniel@0: % generates a sample of latent data points and sets the centres (and Daniel@0: % widths if appropriate) of NET.RBFNET. Daniel@0: % Daniel@0: % If the SAMPTYPE is 'REGULAR', then regular grids of latent data Daniel@0: % points and RBF centres are created. The dimension of the latent data Daniel@0: % space must be 1 or 2. For one-dimensional latent space, the Daniel@0: % LSAMPSIZE parameter gives the number of latent points and the Daniel@0: % RBFSAMPSIZE parameter gives the number of RBF centres. For a two- Daniel@0: % dimensional latent space, these parameters must be vectors of length Daniel@0: % 2 with the number of points in each of the x and y directions to Daniel@0: % create a rectangular grid. The widths of the RBF basis functions are Daniel@0: % set by a call to RBFSETFW passing OPTIONS(7) as the scaling Daniel@0: % parameter. Daniel@0: % Daniel@0: % If the SAMPTYPE is 'UNIFORM' or 'GAUSSIAN' then the latent data is Daniel@0: % found by sampling from a uniform or Gaussian distribution Daniel@0: % correspondingly. The RBF basis function parameters are set by a call Daniel@0: % to RBFSETBF with the DATA parameter as dataset and the OPTIONS Daniel@0: % vector. Daniel@0: % Daniel@0: % Finally, the output layer weights of the RBF are initialised by Daniel@0: % mapping the mean of the latent variable to the mean of the target Daniel@0: % variable, and the L-dimensional latent variale variance to the Daniel@0: % variance of the targets along the first L principal components. Daniel@0: % Daniel@0: % See also Daniel@0: % GTM, GTMEM, PCA, RBFSETBF, RBFSETFW Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: % Check for consistency Daniel@0: errstring = consist(net, 'gtm', data); Daniel@0: if ~isempty(errstring) Daniel@0: error(errstring); Daniel@0: end Daniel@0: Daniel@0: % Check type of sample Daniel@0: stypes = {'regular', 'uniform', 'gaussian'}; Daniel@0: if (strcmp(samp_type, stypes)) == 0 Daniel@0: error('Undefined sample type.') Daniel@0: end Daniel@0: Daniel@0: if net.dim_latent > size(data, 2) Daniel@0: error('Latent space dimension must not be greater than data dimension') Daniel@0: end Daniel@0: nlatent = net.gmmnet.ncentres; Daniel@0: nhidden = net.rbfnet.nhidden; Daniel@0: Daniel@0: % Create latent data sample and set RBF centres Daniel@0: Daniel@0: switch samp_type Daniel@0: case 'regular' Daniel@0: if nargin ~= 6 Daniel@0: error('Regular type must specify latent and RBF shapes'); Daniel@0: end Daniel@0: l_samp_size = varargin{1}; Daniel@0: rbf_samp_size = varargin{2}; Daniel@0: if round(l_samp_size) ~= l_samp_size Daniel@0: error('Latent sample specification must contain integers') Daniel@0: end Daniel@0: % Check existence and size of rbf specification Daniel@0: if any(size(rbf_samp_size) ~= [1 net.dim_latent]) | ... Daniel@0: prod(rbf_samp_size) ~= nhidden Daniel@0: error('Incorrect specification of RBF centres') Daniel@0: end Daniel@0: % Check dimension and type of latent data specification Daniel@0: if any(size(l_samp_size) ~= [1 net.dim_latent]) | ... Daniel@0: prod(l_samp_size) ~= nlatent Daniel@0: error('Incorrect dimension of latent sample spec.') Daniel@0: end Daniel@0: if net.dim_latent == 1 Daniel@0: net.X = [-1:2/(l_samp_size-1):1]'; Daniel@0: net.rbfnet.c = [-1:2/(rbf_samp_size-1):1]'; Daniel@0: net.rbfnet = rbfsetfw(net.rbfnet, options(7)); Daniel@0: elseif net.dim_latent == 2 Daniel@0: net.X = gtm_rctg(l_samp_size); Daniel@0: net.rbfnet.c = gtm_rctg(rbf_samp_size); Daniel@0: net.rbfnet = rbfsetfw(net.rbfnet, options(7)); Daniel@0: else Daniel@0: error('For regular sample, input dimension must be 1 or 2.') Daniel@0: end Daniel@0: Daniel@0: Daniel@0: case {'uniform', 'gaussian'} Daniel@0: if strcmp(samp_type, 'uniform') Daniel@0: net.X = 2 * (rand(nlatent, net.dim_latent) - 0.5); Daniel@0: else Daniel@0: % Sample from N(0, 0.25) distribution to ensure most latent Daniel@0: % data is inside square Daniel@0: net.X = randn(nlatent, net.dim_latent)/2; Daniel@0: end Daniel@0: net.rbfnet = rbfsetbf(net.rbfnet, options, net.X); Daniel@0: otherwise Daniel@0: % Shouldn't get here Daniel@0: error('Invalid sample type'); Daniel@0: Daniel@0: end Daniel@0: Daniel@0: % Latent data sample and basis function parameters chosen. Daniel@0: % Now set output weights Daniel@0: [PCcoeff, PCvec] = pca(data); Daniel@0: Daniel@0: % Scale PCs by eigenvalues Daniel@0: A = PCvec(:, 1:net.dim_latent)*diag(sqrt(PCcoeff(1:net.dim_latent))); Daniel@0: Daniel@0: [temp, Phi] = rbffwd(net.rbfnet, net.X); Daniel@0: % Normalise X to ensure 1:1 mapping of variances and calculate weights Daniel@0: % as solution of Phi*W = normX*A' Daniel@0: normX = (net.X - ones(size(net.X))*diag(mean(net.X)))*diag(1./std(net.X)); Daniel@0: net.rbfnet.w2 = Phi \ (normX*A'); Daniel@0: % Bias is mean of target data Daniel@0: net.rbfnet.b2 = mean(data); Daniel@0: Daniel@0: % Must also set initial value of variance Daniel@0: % Find average distance between nearest centres Daniel@0: % Ensure that distance of centre to itself is excluded by setting diagonal Daniel@0: % entries to realmax Daniel@0: net.gmmnet.centres = rbffwd(net.rbfnet, net.X); Daniel@0: d = dist2(net.gmmnet.centres, net.gmmnet.centres) + ... Daniel@0: diag(ones(net.gmmnet.ncentres, 1)*realmax); Daniel@0: sigma = mean(min(d))/2; Daniel@0: Daniel@0: % Now set covariance to minimum of this and next largest eigenvalue Daniel@0: if net.dim_latent < size(data, 2) Daniel@0: sigma = min(sigma, PCcoeff(net.dim_latent+1)); Daniel@0: end Daniel@0: net.gmmnet.covars = sigma*ones(1, net.gmmnet.ncentres); Daniel@0: Daniel@0: % Sub-function to create the sample data in 2d Daniel@0: function sample = gtm_rctg(samp_size) Daniel@0: Daniel@0: xDim = samp_size(1); Daniel@0: yDim = samp_size(2); Daniel@0: % Produce a grid with the right number of rows and columns Daniel@0: [X, Y] = meshgrid([0:1:(xDim-1)], [(yDim-1):-1:0]); Daniel@0: Daniel@0: % Change grid representation Daniel@0: sample = [X(:), Y(:)]; Daniel@0: Daniel@0: % Shift grid to correct position and scale it Daniel@0: maxXY= max(sample); Daniel@0: sample(:,1) = 2*(sample(:,1) - maxXY(1)/2)./maxXY(1); Daniel@0: sample(:,2) = 2*(sample(:,2) - maxXY(2)/2)./maxXY(2); Daniel@0: return; Daniel@0: Daniel@0: Daniel@0: