Daniel@0: function hdv = mlphdotv(net, x, t, v) Daniel@0: %MLPHDOTV Evaluate the product of the data Hessian with a vector. Daniel@0: % Daniel@0: % Description Daniel@0: % Daniel@0: % HDV = MLPHDOTV(NET, X, T, V) takes an MLP network data structure NET, Daniel@0: % together with the matrix X of input vectors, the matrix T of target Daniel@0: % vectors and an arbitrary row vector V whose length equals the number Daniel@0: % of parameters in the network, and returns the product of the data- Daniel@0: % dependent contribution to the Hessian matrix with V. The Daniel@0: % implementation is based on the R-propagation algorithm of Daniel@0: % Pearlmutter. Daniel@0: % Daniel@0: % See also Daniel@0: % MLP, MLPHESS, HESSCHEK Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) 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: Daniel@0: ndata = size(x, 1); Daniel@0: Daniel@0: [y, z] = mlpfwd(net, x); % Standard forward propagation. Daniel@0: zprime = (1 - z.*z); % Hidden unit first derivatives. Daniel@0: zpprime = -2.0*z.*zprime; % Hidden unit second derivatives. Daniel@0: Daniel@0: vnet = mlpunpak(net, v); % Unpack the v vector. Daniel@0: Daniel@0: % Do the R-forward propagation. Daniel@0: Daniel@0: ra1 = x*vnet.w1 + ones(ndata, 1)*vnet.b1; Daniel@0: rz = zprime.*ra1; Daniel@0: ra2 = rz*net.w2 + z*vnet.w2 + ones(ndata, 1)*vnet.b2; Daniel@0: Daniel@0: switch net.outfn Daniel@0: Daniel@0: case 'linear' % Linear outputs Daniel@0: Daniel@0: ry = ra2; Daniel@0: Daniel@0: case 'logistic' % Logistic outputs Daniel@0: Daniel@0: ry = y.*(1 - y).*ra2; Daniel@0: Daniel@0: case 'softmax' % Softmax outputs Daniel@0: Daniel@0: nout = size(t, 2); Daniel@0: ry = y.*ra2 - y.*(sum(y.*ra2, 2)*ones(1, nout)); Daniel@0: Daniel@0: otherwise Daniel@0: error(['Unknown activation function ', net.outfn]); Daniel@0: end Daniel@0: Daniel@0: % Evaluate delta for the output units. Daniel@0: Daniel@0: delout = y - t; Daniel@0: Daniel@0: % Do the standard backpropagation. Daniel@0: Daniel@0: delhid = zprime.*(delout*net.w2'); Daniel@0: Daniel@0: % Now do the R-backpropagation. Daniel@0: Daniel@0: rdelhid = zpprime.*ra1.*(delout*net.w2') + zprime.*(delout*vnet.w2') + ... Daniel@0: zprime.*(ry*net.w2'); Daniel@0: Daniel@0: % Finally, evaluate the components of hdv and then merge into long vector. Daniel@0: Daniel@0: hw1 = x'*rdelhid; Daniel@0: hb1 = sum(rdelhid, 1); Daniel@0: hw2 = z'*ry + rz'*delout; Daniel@0: hb2 = sum(ry, 1); Daniel@0: Daniel@0: hdv = [hw1(:)', hb1, hw2(:)', hb2];