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