comparison toolboxes/FullBNT-1.0.7/netlabKPM/mlperr_weighted.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_weighted(net, x, t, eso_w)
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-9)
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.actfn
31
32 case 'linear' %Linear outputs
33
34 edata = 0.5*sum(sum((y - t).^2));
35
36 case 'logistic' % Logistic outputs
37
38 % Ensure that log(1-y) is computable: need exp(a) > eps
39 maxcut = -log(eps);
40 % Ensure that log(y) is computable
41 mincut = -log(1/realmin - 1);
42 a = min(a, maxcut);
43 a = max(a, mincut);
44 y = 1./(1 + exp(-a));
45 edata = - sum(sum(t.*log(y) + (1 - t).*log(1 - y)));
46
47 case 'softmax' % Softmax outputs
48
49 nout = size(a,2);
50 % Ensure that sum(exp(a), 2) does not overflow
51 maxcut = log(realmax) - log(nout);
52 % Ensure that exp(a) > 0
53 mincut = log(realmin);
54 a = min(a, maxcut);
55 a = max(a, mincut);
56 temp = exp(a);
57 y = temp./(sum(temp, 2)*ones(1,nout));
58 % Ensure that log(y) is computable
59 y(y<realmin) = realmin;
60 e_app=sum(t.*log(y),2);
61 edata = - eso_w'*e_app;
62 clear e_app;
63
64 otherwise
65 error(['Unknown activation function ', net.actfn]);
66 end
67
68 [e, edata, eprior] = errbayes(net, edata);