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