wolffd@0: function [e, edata, eprior] = mlperr(net, x, t) wolffd@0: %MLPERR Evaluate error function for 2-layer network. wolffd@0: % wolffd@0: % Description wolffd@0: % E = MLPERR(NET, X, T) takes a network data structure NET together wolffd@0: % with a matrix X of input vectors and a matrix T of target vectors, wolffd@0: % and evaluates the error function E. The choice of error function wolffd@0: % corresponds to the output unit activation function. Each row of X wolffd@0: % corresponds to one input vector and each row of T corresponds to one wolffd@0: % target vector. wolffd@0: % wolffd@0: % [E, EDATA, EPRIOR] = MLPERR(NET, X, T) additionally returns the data wolffd@0: % and prior components of the error, assuming a zero mean Gaussian wolffd@0: % prior on the weights with inverse variance parameters ALPHA and BETA wolffd@0: % taken from the network data structure NET. wolffd@0: % wolffd@0: % See also wolffd@0: % MLP, MLPPAK, MLPUNPAK, MLPFWD, MLPBKP, MLPGRAD wolffd@0: % wolffd@0: wolffd@0: % Copyright (c) Ian T Nabney (1996-2001) wolffd@0: wolffd@0: % Check arguments for consistency wolffd@0: errstring = consist(net, 'mlp', x, t); wolffd@0: if ~isempty(errstring); wolffd@0: error(errstring); wolffd@0: end wolffd@0: [y, z, a] = mlpfwd(net, x); wolffd@0: wolffd@0: switch net.outfn wolffd@0: wolffd@0: case 'linear' % Linear outputs wolffd@0: edata = 0.5*sum(sum((y - t).^2)); wolffd@0: wolffd@0: case 'logistic' % Logistic outputs wolffd@0: % Ensure that log(1-y) is computable: need exp(a) > eps wolffd@0: maxcut = -log(eps); wolffd@0: % Ensure that log(y) is computable wolffd@0: mincut = -log(1/realmin - 1); wolffd@0: a = min(a, maxcut); wolffd@0: a = max(a, mincut); wolffd@0: y = 1./(1 + exp(-a)); wolffd@0: edata = - sum(sum(t.*log(y) + (1 - t).*log(1 - y))); wolffd@0: wolffd@0: case 'softmax' % Softmax outputs wolffd@0: nout = size(a,2); wolffd@0: % Ensure that sum(exp(a), 2) does not overflow wolffd@0: maxcut = log(realmax) - log(nout); wolffd@0: % Ensure that exp(a) > 0 wolffd@0: mincut = log(realmin); wolffd@0: a = min(a, maxcut); wolffd@0: a = max(a, mincut); wolffd@0: temp = exp(a); wolffd@0: y = temp./(sum(temp, 2)*ones(1,nout)); wolffd@0: % Ensure that log(y) is computable wolffd@0: y(y