annotate toolboxes/FullBNT-1.0.7/netlabKPM/mlperr_weighted.m @ 0:cc4b1211e677 tip

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