comparison toolboxes/FullBNT-1.0.7/netlab3.3/mlperr.m @ 0:e9a9cd732c1e tip

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