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