wolffd@0
|
1 function [e, edata, eprior] = mlperr(net, x, t)
|
wolffd@0
|
2 %MLPERR Evaluate error function for 2-layer network.
|
wolffd@0
|
3 %
|
wolffd@0
|
4 % Description
|
wolffd@0
|
5 % E = MLPERR(NET, X, T) takes a network data structure NET together
|
wolffd@0
|
6 % with a matrix X of input vectors and a matrix T of target vectors,
|
wolffd@0
|
7 % and evaluates the error function E. The choice of error function
|
wolffd@0
|
8 % corresponds to the output unit activation function. Each row of X
|
wolffd@0
|
9 % corresponds to one input vector and each row of T corresponds to one
|
wolffd@0
|
10 % target vector.
|
wolffd@0
|
11 %
|
wolffd@0
|
12 % [E, EDATA, EPRIOR] = MLPERR(NET, X, T) additionally returns the data
|
wolffd@0
|
13 % and prior components of the error, assuming a zero mean Gaussian
|
wolffd@0
|
14 % prior on the weights with inverse variance parameters ALPHA and BETA
|
wolffd@0
|
15 % taken from the network data structure NET.
|
wolffd@0
|
16 %
|
wolffd@0
|
17 % See also
|
wolffd@0
|
18 % MLP, MLPPAK, MLPUNPAK, MLPFWD, MLPBKP, MLPGRAD
|
wolffd@0
|
19 %
|
wolffd@0
|
20
|
wolffd@0
|
21 % Copyright (c) Ian T Nabney (1996-2001)
|
wolffd@0
|
22
|
wolffd@0
|
23 % Check arguments for consistency
|
wolffd@0
|
24 errstring = consist(net, 'mlp', x, t);
|
wolffd@0
|
25 if ~isempty(errstring);
|
wolffd@0
|
26 error(errstring);
|
wolffd@0
|
27 end
|
wolffd@0
|
28 [y, z, a] = mlpfwd(net, x);
|
wolffd@0
|
29
|
wolffd@0
|
30 switch net.outfn
|
wolffd@0
|
31
|
wolffd@0
|
32 case 'linear' % Linear outputs
|
wolffd@0
|
33 edata = 0.5*sum(sum((y - t).^2));
|
wolffd@0
|
34
|
wolffd@0
|
35 case 'logistic' % Logistic outputs
|
wolffd@0
|
36 % Ensure that log(1-y) is computable: need exp(a) > eps
|
wolffd@0
|
37 maxcut = -log(eps);
|
wolffd@0
|
38 % Ensure that log(y) is computable
|
wolffd@0
|
39 mincut = -log(1/realmin - 1);
|
wolffd@0
|
40 a = min(a, maxcut);
|
wolffd@0
|
41 a = max(a, mincut);
|
wolffd@0
|
42 y = 1./(1 + exp(-a));
|
wolffd@0
|
43 edata = - sum(sum(t.*log(y) + (1 - t).*log(1 - y)));
|
wolffd@0
|
44
|
wolffd@0
|
45 case 'softmax' % Softmax outputs
|
wolffd@0
|
46 nout = size(a,2);
|
wolffd@0
|
47 % Ensure that sum(exp(a), 2) does not overflow
|
wolffd@0
|
48 maxcut = log(realmax) - log(nout);
|
wolffd@0
|
49 % Ensure that exp(a) > 0
|
wolffd@0
|
50 mincut = log(realmin);
|
wolffd@0
|
51 a = min(a, maxcut);
|
wolffd@0
|
52 a = max(a, mincut);
|
wolffd@0
|
53 temp = exp(a);
|
wolffd@0
|
54 y = temp./(sum(temp, 2)*ones(1,nout));
|
wolffd@0
|
55 % Ensure that log(y) is computable
|
wolffd@0
|
56 y(y<realmin) = realmin;
|
wolffd@0
|
57 edata = - sum(sum(t.*log(y)));
|
wolffd@0
|
58
|
wolffd@0
|
59 otherwise
|
wolffd@0
|
60 error(['Unknown activation function ', net.outfn]);
|
wolffd@0
|
61 end
|
wolffd@0
|
62 [e, edata, eprior] = errbayes(net, edata);
|