Daniel@0: function net = netinit(net, prior) Daniel@0: %NETINIT Initialise the weights in a network. Daniel@0: % Daniel@0: % Description Daniel@0: % Daniel@0: % NET = NETINIT(NET, PRIOR) takes a network data structure NET and sets Daniel@0: % the weights and biases by sampling from a Gaussian distribution. If Daniel@0: % PRIOR is a scalar, then all of the parameters (weights and biases) Daniel@0: % are sampled from a single isotropic Gaussian with inverse variance Daniel@0: % equal to PRIOR. If PRIOR is a data structure of the kind generated by Daniel@0: % MLPPRIOR, then the parameters are sampled from multiple Gaussians Daniel@0: % according to their groupings (defined by the INDEX field) with Daniel@0: % corresponding variances (defined by the ALPHA field). Daniel@0: % Daniel@0: % See also Daniel@0: % MLPPRIOR, NETUNPAK, RBFPRIOR Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: if isstruct(prior) Daniel@0: if (isfield(net, 'mask')) Daniel@0: if find(sum(prior.index, 2)) ~= find(net.mask) Daniel@0: error('Index does not match mask'); Daniel@0: end Daniel@0: sig = sqrt(prior.index*prior.alpha); Daniel@0: % Weights corresponding to zeros in mask will not be used anyway Daniel@0: % Set their priors to one to avoid division by zero Daniel@0: sig = sig + (sig == 0); Daniel@0: sig = 1./sqrt(sig); Daniel@0: else Daniel@0: sig = 1./sqrt(prior.index*prior.alpha); Daniel@0: end Daniel@0: w = sig'.*randn(1, net.nwts); Daniel@0: elseif size(prior) == [1 1] Daniel@0: w = randn(1, net.nwts).*sqrt(1/prior); Daniel@0: else Daniel@0: error('prior must be a scalar or a structure'); Daniel@0: end Daniel@0: Daniel@0: if (isfield(net, 'mask')) Daniel@0: w = w(logical(net.mask)); Daniel@0: end Daniel@0: net = netunpak(net, w); Daniel@0: