wolffd@0
|
1 function net = netinit(net, prior)
|
wolffd@0
|
2 %NETINIT Initialise the weights in a network.
|
wolffd@0
|
3 %
|
wolffd@0
|
4 % Description
|
wolffd@0
|
5 %
|
wolffd@0
|
6 % NET = NETINIT(NET, PRIOR) takes a network data structure NET and sets
|
wolffd@0
|
7 % the weights and biases by sampling from a Gaussian distribution. If
|
wolffd@0
|
8 % PRIOR is a scalar, then all of the parameters (weights and biases)
|
wolffd@0
|
9 % are sampled from a single isotropic Gaussian with inverse variance
|
wolffd@0
|
10 % equal to PRIOR. If PRIOR is a data structure of the kind generated by
|
wolffd@0
|
11 % MLPPRIOR, then the parameters are sampled from multiple Gaussians
|
wolffd@0
|
12 % according to their groupings (defined by the INDEX field) with
|
wolffd@0
|
13 % corresponding variances (defined by the ALPHA field).
|
wolffd@0
|
14 %
|
wolffd@0
|
15 % See also
|
wolffd@0
|
16 % MLPPRIOR, NETUNPAK, RBFPRIOR
|
wolffd@0
|
17 %
|
wolffd@0
|
18
|
wolffd@0
|
19 % Copyright (c) Ian T Nabney (1996-2001)
|
wolffd@0
|
20
|
wolffd@0
|
21 if isstruct(prior)
|
wolffd@0
|
22 if (isfield(net, 'mask'))
|
wolffd@0
|
23 if find(sum(prior.index, 2)) ~= find(net.mask)
|
wolffd@0
|
24 error('Index does not match mask');
|
wolffd@0
|
25 end
|
wolffd@0
|
26 sig = sqrt(prior.index*prior.alpha);
|
wolffd@0
|
27 % Weights corresponding to zeros in mask will not be used anyway
|
wolffd@0
|
28 % Set their priors to one to avoid division by zero
|
wolffd@0
|
29 sig = sig + (sig == 0);
|
wolffd@0
|
30 sig = 1./sqrt(sig);
|
wolffd@0
|
31 else
|
wolffd@0
|
32 sig = 1./sqrt(prior.index*prior.alpha);
|
wolffd@0
|
33 end
|
wolffd@0
|
34 w = sig'.*randn(1, net.nwts);
|
wolffd@0
|
35 elseif size(prior) == [1 1]
|
wolffd@0
|
36 w = randn(1, net.nwts).*sqrt(1/prior);
|
wolffd@0
|
37 else
|
wolffd@0
|
38 error('prior must be a scalar or a structure');
|
wolffd@0
|
39 end
|
wolffd@0
|
40
|
wolffd@0
|
41 if (isfield(net, 'mask'))
|
wolffd@0
|
42 w = w(logical(net.mask));
|
wolffd@0
|
43 end
|
wolffd@0
|
44 net = netunpak(net, w);
|
wolffd@0
|
45
|