annotate toolboxes/FullBNT-1.0.7/netlab3.3/glm.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function net = glm(nin, nout, outfunc, prior, beta)
wolffd@0 2 %GLM Create a generalized linear model.
wolffd@0 3 %
wolffd@0 4 % Description
wolffd@0 5 %
wolffd@0 6 % NET = GLM(NIN, NOUT, FUNC) takes the number of inputs and outputs for
wolffd@0 7 % a generalized linear model, together with a string FUNC which
wolffd@0 8 % specifies the output unit activation function, and returns a data
wolffd@0 9 % structure NET. The weights are drawn from a zero mean, isotropic
wolffd@0 10 % Gaussian, with variance scaled by the fan-in of the output units.
wolffd@0 11 % This makes use of the Matlab function RANDN and so the seed for the
wolffd@0 12 % random weight initialization can be set using RANDN('STATE', S)
wolffd@0 13 % where S is the seed value. The optional argument ALPHA sets the
wolffd@0 14 % inverse variance for the weight initialization.
wolffd@0 15 %
wolffd@0 16 % The fields in NET are
wolffd@0 17 % type = 'glm'
wolffd@0 18 % nin = number of inputs
wolffd@0 19 % nout = number of outputs
wolffd@0 20 % nwts = total number of weights and biases
wolffd@0 21 % actfn = string describing the output unit activation function:
wolffd@0 22 % 'linear'
wolffd@0 23 % 'logistic'
wolffd@0 24 % 'softmax'
wolffd@0 25 % w1 = first-layer weight matrix
wolffd@0 26 % b1 = first-layer bias vector
wolffd@0 27 %
wolffd@0 28 % NET = GLM(NIN, NOUT, FUNC, PRIOR), in which PRIOR is a scalar, allows
wolffd@0 29 % the field NET.ALPHA in the data structure NET to be set,
wolffd@0 30 % corresponding to a zero-mean isotropic Gaussian prior with inverse
wolffd@0 31 % variance with value PRIOR. Alternatively, PRIOR can consist of a data
wolffd@0 32 % structure with fields ALPHA and INDEX, allowing individual Gaussian
wolffd@0 33 % priors to be set over groups of weights in the network. Here ALPHA is
wolffd@0 34 % a column vector in which each element corresponds to a separate
wolffd@0 35 % group of weights, which need not be mutually exclusive. The
wolffd@0 36 % membership of the groups is defined by the matrix INDEX in which the
wolffd@0 37 % columns correspond to the elements of ALPHA. Each column has one
wolffd@0 38 % element for each weight in the matrix, in the order defined by the
wolffd@0 39 % function GLMPAK, and each element is 1 or 0 according to whether the
wolffd@0 40 % weight is a member of the corresponding group or not.
wolffd@0 41 %
wolffd@0 42 % NET = GLM(NIN, NOUT, FUNC, PRIOR, BETA) also sets the additional
wolffd@0 43 % field NET.BETA in the data structure NET, where beta corresponds to
wolffd@0 44 % the inverse noise variance.
wolffd@0 45 %
wolffd@0 46 % See also
wolffd@0 47 % GLMPAK, GLMUNPAK, GLMFWD, GLMERR, GLMGRAD, GLMTRAIN
wolffd@0 48 %
wolffd@0 49
wolffd@0 50 % Copyright (c) Ian T Nabney (1996-2001)
wolffd@0 51
wolffd@0 52 net.type = 'glm';
wolffd@0 53 net.nin = nin;
wolffd@0 54 net.nout = nout;
wolffd@0 55 net.nwts = (nin + 1)*nout;
wolffd@0 56
wolffd@0 57 outtfns = {'linear', 'logistic', 'softmax'};
wolffd@0 58
wolffd@0 59 if sum(strcmp(outfunc, outtfns)) == 0
wolffd@0 60 error('Undefined activation function. Exiting.');
wolffd@0 61 else
wolffd@0 62 net.outfn = outfunc;
wolffd@0 63 end
wolffd@0 64
wolffd@0 65 if nargin > 3
wolffd@0 66 if isstruct(prior)
wolffd@0 67 net.alpha = prior.alpha;
wolffd@0 68 net.index = prior.index;
wolffd@0 69 elseif size(prior) == [1 1]
wolffd@0 70 net.alpha = prior;
wolffd@0 71 else
wolffd@0 72 error('prior must be a scalar or structure');
wolffd@0 73 end
wolffd@0 74 end
wolffd@0 75
wolffd@0 76 net.w1 = randn(nin, nout)/sqrt(nin + 1);
wolffd@0 77 net.b1 = randn(1, nout)/sqrt(nin + 1);
wolffd@0 78
wolffd@0 79 if nargin == 5
wolffd@0 80 net.beta = beta;
wolffd@0 81 end
wolffd@0 82