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