├── Contents.m ├── LICENSE ├── README.md ├── conffig.m ├── confmat.m ├── conjgrad.m ├── consist.m ├── datread.m ├── datwrite.m ├── dem2ddat.m ├── demard.m ├── demev1.m ├── demev2.m ├── demev3.m ├── demgauss.m ├── demglm1.m ├── demglm2.m ├── demgmm1.m ├── demgmm2.m ├── demgmm3.m ├── demgmm4.m ├── demgmm5.m ├── demgp.m ├── demgpard.m ├── demgpot.m ├── demgtm1.m ├── demgtm2.m ├── demhint.m ├── demhmc1.m ├── demhmc2.m ├── demhmc3.m ├── demkmn1.m ├── demknn1.m ├── demmdn1.m ├── demmet1.m ├── demmlp1.m ├── demmlp2.m ├── demnlab.m ├── demns1.m ├── demolgd1.m ├── demopt1.m ├── dempot.m ├── demprgp.m ├── demprior.m ├── demrbf1.m ├── demsom1.m ├── demtrain.m ├── dist2.m ├── eigdec.m ├── errbayes.m ├── evidence.m ├── fevbayes.m ├── foptions.m ├── gauss.m ├── gbayes.m ├── glm.m ├── glmderiv.m ├── glmerr.m ├── glmevfwd.m ├── glmfwd.m ├── glmgrad.m ├── glmhess.m ├── glminit.m ├── glmpak.m ├── glmtrain.m ├── glmunpak.m ├── gmm.m ├── gmmactiv.m ├── gmmem.m ├── gmminit.m ├── gmmpak.m ├── gmmpost.m ├── gmmprob.m ├── gmmsamp.m ├── gmmunpak.m ├── gp.m ├── gpcovar.m ├── gpcovarf.m ├── gpcovarp.m ├── gperr.m ├── gpfwd.m ├── gpgrad.m ├── gpinit.m ├── gppak.m ├── gpunpak.m ├── gradchek.m ├── graddesc.m ├── gsamp.m ├── gtm.m ├── gtmem.m ├── gtmfwd.m ├── gtminit.m ├── gtmlmean.m ├── gtmlmode.m ├── gtmmag.m ├── gtmpost.m ├── gtmprob.m ├── hbayes.m ├── help ├── conffig.htm ├── confmat.htm ├── conjgrad.htm ├── consist.htm ├── datread.htm ├── datwrite.htm ├── dem2ddat.htm ├── demard.htm ├── demev1.htm ├── demev2.htm ├── demev3.htm ├── demgauss.htm ├── demglm1.htm ├── demglm2.htm ├── demgmm1.htm ├── demgmm2.htm ├── demgmm3.htm ├── demgmm4.htm ├── demgmm5.htm ├── demgp.htm ├── demgpard.htm ├── demgpot.htm ├── demgtm1.htm ├── demgtm2.htm ├── demhint.htm ├── demhmc1.htm ├── demhmc2.htm ├── demhmc3.htm ├── demkmn1.htm ├── demknn1.htm ├── demmdn1.htm ├── demmet1.htm ├── demmlp1.htm ├── demmlp2.htm ├── demnlab.htm ├── demns1.htm ├── demolgd1.htm ├── demopt1.htm ├── dempot.htm ├── demprgp.htm ├── demprior.htm ├── demrbf1.htm ├── demsom1.htm ├── demtrain.htm ├── dist2.htm ├── eigdec.htm ├── errbayes.htm ├── evidence.htm ├── fevbayes.htm ├── gauss.htm ├── gbayes.htm ├── glm.htm ├── glmderiv.htm ├── glmerr.htm ├── glmevfwd.htm ├── glmfwd.htm ├── glmgrad.htm ├── glmhess.htm ├── glminit.htm ├── glmpak.htm ├── glmtrain.htm ├── glmunpak.htm ├── gmm.htm ├── gmmactiv.htm ├── gmmem.htm ├── gmminit.htm ├── gmmpak.htm ├── gmmpost.htm ├── gmmprob.htm ├── gmmsamp.htm ├── gmmunpak.htm ├── gp.htm ├── gpcovar.htm ├── gpcovarf.htm ├── gpcovarp.htm ├── gperr.htm ├── gpfwd.htm ├── gpgrad.htm ├── gpinit.htm ├── gppak.htm ├── gpunpak.htm ├── gradchek.htm ├── graddesc.htm ├── gsamp.htm ├── gtm.htm ├── gtmem.htm ├── gtmfwd.htm ├── gtminit.htm ├── gtmlmean.htm ├── gtmlmode.htm ├── gtmmag.htm ├── gtmpost.htm ├── gtmprob.htm ├── hbayes.htm ├── hesschek.htm ├── hintmat.htm ├── hinton.htm ├── histp.htm ├── hmc.htm ├── index.htm ├── kmeans.htm ├── knn.htm ├── knnfwd.htm ├── linef.htm ├── linemin.htm ├── mdn.htm ├── mdn2gmm.htm ├── mdndist2.htm ├── mdnerr.htm ├── mdnfwd.htm ├── mdngrad.htm ├── mdninit.htm ├── mdnpak.htm ├── mdnpost.htm ├── mdnprob.htm ├── mdnunpak.htm ├── metrop.htm ├── minbrack.htm ├── mlp.htm ├── mlpbkp.htm ├── mlpderiv.htm ├── mlperr.htm ├── mlpevfwd.htm ├── mlpfwd.htm ├── mlpgrad.htm ├── mlphdotv.htm ├── mlphess.htm ├── mlphint.htm ├── mlpinit.htm ├── mlppak.htm ├── mlpprior.htm ├── mlptrain.htm ├── mlpunpak.htm ├── netderiv.htm ├── neterr.htm ├── netevfwd.htm ├── netgrad.htm ├── nethess.htm ├── netinit.htm ├── netopt.htm ├── netpak.htm ├── netunpak.htm ├── olgd.htm ├── pca.htm ├── plotmat.htm ├── ppca.htm ├── quasinew.htm ├── rbf.htm ├── rbfbkp.htm ├── rbfderiv.htm ├── rbferr.htm ├── rbfevfwd.htm ├── rbffwd.htm ├── rbfgrad.htm ├── rbfhess.htm ├── rbfjacob.htm ├── rbfpak.htm ├── rbfprior.htm ├── rbfsetbf.htm ├── rbfsetfw.htm ├── rbftrain.htm ├── rbfunpak.htm ├── rosegrad.htm ├── rosen.htm ├── scg.htm ├── som.htm ├── somfwd.htm ├── sompak.htm ├── somtrain.htm └── somunpak.htm ├── hesschek.m ├── hintmat.m ├── hinton.m ├── histp.m ├── hmc.m ├── kmeans.m ├── knn.m ├── knnfwd.m ├── linef.m ├── linemin.m ├── maxitmess.m ├── mdn.m ├── mdn2gmm.m ├── mdndist2.m ├── mdnerr.m ├── mdnfwd.m ├── mdngrad.m ├── mdninit.m ├── mdnnet.mat ├── mdnpak.m ├── mdnpost.m ├── mdnprob.m ├── mdnunpak.m ├── metrop.m ├── minbrack.m ├── mlp.m ├── mlpbkp.m ├── mlpderiv.m ├── mlperr.m ├── mlpevfwd.m ├── mlpfwd.m ├── mlpgrad.m ├── mlphdotv.m ├── mlphess.m ├── mlphint.m ├── mlpinit.m ├── mlppak.m ├── mlpprior.m ├── mlptrain.m ├── mlpunpak.m ├── netderiv.m ├── neterr.m ├── netevfwd.m ├── netgrad.m ├── nethess.m ├── netinit.m ├── netlogo.mat ├── netopt.m ├── netpak.m ├── netunpak.m ├── oilTrn.dat ├── oilTst.dat ├── olgd.m ├── pca.m ├── plotmat.m ├── ppca.m ├── quasinew.m ├── rbf.m ├── rbfbkp.m ├── rbfderiv.m ├── rbferr.m ├── rbfevfwd.m ├── rbffwd.m ├── rbfgrad.m ├── rbfhess.m ├── rbfjacob.m ├── rbfpak.m ├── rbfprior.m ├── rbfsetbf.m ├── rbfsetfw.m ├── rbftrain.m ├── rbfunpak.m ├── rosegrad.m ├── rosen.m ├── scg.m ├── som.m ├── somfwd.m ├── sompak.m ├── somtrain.m ├── somunpak.m └── xor.dat /README.md: -------------------------------------------------------------------------------- 1 | # netlab 2 | 3 | NETLAB toolbox hosted on GitHub for convenience. In the longer term the aim is to work with Ian to make an updated version of netlab available via GitHub. Watch this space. 4 | 5 | Here is [the original site](http://www.aston.ac.uk/eas/research/groups/ncrg/resources/netlab/downloads/). 6 | -------------------------------------------------------------------------------- /conffig.m: -------------------------------------------------------------------------------- 1 | function fh=conffig(y, t) 2 | %CONFFIG Display a confusion matrix. 3 | % 4 | % Description 5 | % CONFFIG(Y, T) displays the confusion matrix and classification 6 | % performance for the predictions mat{y} compared with the targets T. 7 | % The data is assumed to be in a 1-of-N encoding, unless there is just 8 | % one column, when it is assumed to be a 2 class problem with a 0-1 9 | % encoding. Each row of Y and T corresponds to a single example. 10 | % 11 | % In the confusion matrix, the rows represent the true classes and the 12 | % columns the predicted classes. 13 | % 14 | % FH = CONFFIG(Y, T) also returns the figure handle FH which can be 15 | % used, for instance, to delete the figure when it is no longer needed. 16 | % 17 | % See also 18 | % CONFMAT, DEMTRAIN 19 | % 20 | 21 | % Copyright (c) Ian T Nabney (1996-2001) 22 | 23 | [C, rate] = confmat(y, t); 24 | 25 | fh = figure('Name', 'Confusion matrix', ... 26 | 'NumberTitle', 'off'); 27 | 28 | plotmat(C, 'k', 'k', 14); 29 | title(['Classification rate: ' num2str(rate(1)) '%'], 'FontSize', 14); 30 | -------------------------------------------------------------------------------- /confmat.m: -------------------------------------------------------------------------------- 1 | function [C,rate]=confmat(Y,T) 2 | %CONFMAT Compute a confusion matrix. 3 | % 4 | % Description 5 | % [C, RATE] = CONFMAT(Y, T) computes the confusion matrix C and 6 | % classification performance RATE for the predictions mat{y} compared 7 | % with the targets T. The data is assumed to be in a 1-of-N encoding, 8 | % unless there is just one column, when it is assumed to be a 2 class 9 | % problem with a 0-1 encoding. Each row of Y and T corresponds to a 10 | % single example. 11 | % 12 | % In the confusion matrix, the rows represent the true classes and the 13 | % columns the predicted classes. The vector RATE has two entries: the 14 | % percentage of correct classifications and the total number of correct 15 | % classifications. 16 | % 17 | % See also 18 | % CONFFIG, DEMTRAIN 19 | % 20 | 21 | % Copyright (c) Ian T Nabney (1996-2001) 22 | 23 | [n c]=size(Y); 24 | [n2 c2]=size(T); 25 | 26 | if n~=n2 | c~=c2 27 | error('Outputs and targets are different sizes') 28 | end 29 | 30 | if c > 1 31 | % Find the winning class assuming 1-of-N encoding 32 | [maximum Yclass] = max(Y', [], 1); 33 | 34 | TL=[1:c]*T'; 35 | else 36 | % Assume two classes with 0-1 encoding 37 | c = 2; 38 | class2 = find(T > 0.5); 39 | TL = ones(n, 1); 40 | TL(class2) = 2; 41 | class2 = find(Y > 0.5); 42 | Yclass = ones(n, 1); 43 | Yclass(class2) = 2; 44 | end 45 | 46 | % Compute 47 | correct = (Yclass==TL); 48 | total=sum(sum(correct)); 49 | rate=[total*100/n total]; 50 | 51 | C=zeros(c,c); 52 | for i=1:c 53 | for j=1:c 54 | C(i,j) = sum((Yclass==j).*(TL==i)); 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /datwrite.m: -------------------------------------------------------------------------------- 1 | function datwrite(filename, x, t) 2 | %DATWRITE Write data to ascii file. 3 | % 4 | % Description 5 | % 6 | % DATWRITE(FILENAME, X, T) takes a matrix X of input vectors and a 7 | % matrix T of target vectors and writes them to an ascii file named 8 | % FILENAME. The file format is as follows: the first row contains the 9 | % string NIN followed by the number of inputs, the second row contains 10 | % the string NOUT followed by the number of outputs, and the third row 11 | % contains the string NDATA followed by the number of data vectors. 12 | % Subsequent lines each contain one input vector followed by one output 13 | % vector, with individual values separated by spaces. 14 | % 15 | % See also 16 | % DATREAD 17 | % 18 | 19 | % Copyright (c) Ian T Nabney (1996-2001) 20 | 21 | nin = size(x, 2); 22 | nout = size(t, 2); 23 | ndata = size(x, 1); 24 | 25 | fid = fopen(filename, 'wt'); 26 | if fid == -1 27 | error('Failed to open file.') 28 | end 29 | 30 | if size(t, 1) ~= ndata 31 | error('x and t must have same number of rows.'); 32 | end 33 | 34 | fprintf(fid, ' nin %d\n nout %d\n ndata %d\n', nin , nout, ndata); 35 | for i = 1 : ndata 36 | fprintf(fid, '%13e ', x(i,:), t(i,:)); 37 | fprintf(fid, '\n'); 38 | end 39 | 40 | flag = fclose(fid); 41 | if flag == -1 42 | error('Failed to close file.') 43 | end 44 | 45 | -------------------------------------------------------------------------------- /dem2ddat.m: -------------------------------------------------------------------------------- 1 | function [data, c, prior, sd] = dem2ddat(ndata) 2 | %DEM2DDAT Generates two dimensional data for demos. 3 | % 4 | % Description 5 | % The data is drawn from three spherical Gaussian distributions with 6 | % priors 0.3, 0.5 and 0.2; centres (2, 3.5), (0, 0) and (0,2); and 7 | % standard deviations 0.2, 0.5 and 1.0. DATA = DEM2DDAT(NDATA) 8 | % generates NDATA points. 9 | % 10 | % [DATA, C] = DEM2DDAT(NDATA) also returns a matrix containing the 11 | % centres of the Gaussian distributions. 12 | % 13 | % See also 14 | % DEMGMM1, DEMKMEAN, DEMKNN1 15 | % 16 | 17 | % Copyright (c) Ian T Nabney (1996-2001) 18 | 19 | input_dim = 2; 20 | 21 | % Fix seed for reproducible results 22 | randn('state', 42); 23 | 24 | % Generate mixture of three Gaussians in two dimensional space 25 | data = randn(ndata, input_dim); 26 | 27 | % Priors for the three clusters 28 | prior(1) = 0.3; 29 | prior(2) = 0.5; 30 | prior(3) = 0.2; 31 | 32 | % Cluster centres 33 | c = [2.0, 3.5; 0.0, 0.0; 0.0, 2.0]; 34 | 35 | % Cluster standard deviations 36 | sd = [0.2 0.5 1.0]; 37 | 38 | % Put first cluster at (2, 3.5) 39 | data(1:prior(1)*ndata, 1) = data(1:prior(1)*ndata, 1) * 0.2 + c(1,1); 40 | data(1:prior(1)*ndata, 2) = data(1:prior(1)*ndata, 2) * 0.2 + c(1,2); 41 | 42 | % Leave second cluster at (0,0) 43 | data((prior(1)*ndata + 1):(prior(2)+prior(1))*ndata, :) = ... 44 | data((prior(1)*ndata + 1):(prior(2)+prior(1))*ndata, :) * 0.5; 45 | 46 | % Put third cluster at (0,2) 47 | data((prior(1)+prior(2))*ndata +1:ndata, 2) = ... 48 | data((prior(1)+prior(2))*ndata+1:ndata, 2) + c(3, 2); 49 | -------------------------------------------------------------------------------- /demgpot.m: -------------------------------------------------------------------------------- 1 | function g = demgpot(x, mix) 2 | %DEMGPOT Computes the gradient of the negative log likelihood for a mixture model. 3 | % 4 | % Description 5 | % This function computes the gradient of the negative log of the 6 | % unconditional data density P(X) with respect to the coefficients of 7 | % the data vector X for a Gaussian mixture model. The data structure 8 | % MIX defines the mixture model, while the matrix X contains the data 9 | % vector as a row vector. Note the unusual order of the arguments: this 10 | % is so that the function can be used in DEMHMC1 directly for sampling 11 | % from the distribution P(X). 12 | % 13 | % See also 14 | % DEMHMC1, DEMMET1, DEMPOT 15 | % 16 | 17 | % Copyright (c) Ian T Nabney (1996-2001) 18 | 19 | % Computes the potential gradient 20 | 21 | temp = (ones(mix.ncentres,1)*x)-mix.centres; 22 | temp = temp.*(gmmactiv(mix,x)'*ones(1, mix.nin)); 23 | % Assume spherical covariance structure 24 | if ~strcmp(mix.covar_type, 'spherical') 25 | error('Spherical covariance only.') 26 | end 27 | temp = temp./(mix.covars'*ones(1, mix.nin)); 28 | temp = temp.*(mix.priors'*ones(1, mix.nin)); 29 | g = sum(temp, 1)/gmmprob(mix, x); -------------------------------------------------------------------------------- /dempot.m: -------------------------------------------------------------------------------- 1 | function e = dempot(x, mix) 2 | %DEMPOT Computes the negative log likelihood for a mixture model. 3 | % 4 | % Description 5 | % This function computes the negative log of the unconditional data 6 | % density P(X) for a Gaussian mixture model. The data structure MIX 7 | % defines the mixture model, while the matrix X contains the data 8 | % vectors. 9 | % 10 | % See also 11 | % DEMGPOT, DEMHMC1, DEMMET1 12 | % 13 | 14 | % Copyright (c) Ian T Nabney (1996-2001) 15 | 16 | % Computes the potential (negative log likelihood) 17 | e = -log(gmmprob(mix, x)); -------------------------------------------------------------------------------- /dist2.m: -------------------------------------------------------------------------------- 1 | function n2 = dist2(x, c) 2 | %DIST2 Calculates squared distance between two sets of points. 3 | % 4 | % Description 5 | % D = DIST2(X, C) takes two matrices of vectors and calculates the 6 | % squared Euclidean distance between them. Both matrices must be of 7 | % the same column dimension. If X has M rows and N columns, and C has 8 | % L rows and N columns, then the result has M rows and L columns. The 9 | % I, Jth entry is the squared distance from the Ith row of X to the 10 | % Jth row of C. 11 | % 12 | % See also 13 | % GMMACTIV, KMEANS, RBFFWD 14 | % 15 | 16 | % Copyright (c) Ian T Nabney (1996-2001) 17 | 18 | [ndata, dimx] = size(x); 19 | [ncentres, dimc] = size(c); 20 | if dimx ~= dimc 21 | error('Data dimension does not match dimension of centres') 22 | end 23 | 24 | n2 = (ones(ncentres, 1) * sum((x.^2)', 1))' + ... 25 | ones(ndata, 1) * sum((c.^2)',1) - ... 26 | 2.*(x*(c')); 27 | 28 | % Rounding errors occasionally cause negative entries in n2 29 | if any(any(n2<0)) 30 | n2(n2<0) = 0; 31 | end -------------------------------------------------------------------------------- /eigdec.m: -------------------------------------------------------------------------------- 1 | function [evals, evec] = eigdec(x, N) 2 | %EIGDEC Sorted eigendecomposition 3 | % 4 | % Description 5 | % EVALS = EIGDEC(X, N computes the largest N eigenvalues of the 6 | % matrix X in descending order. [EVALS, EVEC] = EIGDEC(X, N) also 7 | % computes the corresponding eigenvectors. 8 | % 9 | % See also 10 | % PCA, PPCA 11 | % 12 | 13 | % Copyright (c) Ian T Nabney (1996-2001) 14 | 15 | if nargout == 1 16 | evals_only = logical(1); 17 | else 18 | evals_only = logical(0); 19 | end 20 | 21 | if N ~= round(N) | N < 1 | N > size(x, 2) 22 | error('Number of PCs must be integer, >0, < dim'); 23 | end 24 | 25 | % Find the eigenvalues of the data covariance matrix 26 | if evals_only 27 | % Use eig function as always more efficient than eigs here 28 | temp_evals = eig(x); 29 | else 30 | % Use eig function unless fraction of eigenvalues required is tiny 31 | if (N/size(x, 2)) > 0.04 32 | [temp_evec, temp_evals] = eig(x); 33 | else 34 | options.disp = 0; 35 | [temp_evec, temp_evals] = eigs(x, N, 'LM', options); 36 | end 37 | temp_evals = diag(temp_evals); 38 | end 39 | 40 | % Eigenvalues nearly always returned in descending order, but just 41 | % to make sure..... 42 | [evals perm] = sort(-temp_evals); 43 | evals = -evals(1:N); 44 | if ~evals_only 45 | if evals == temp_evals(1:N) 46 | % Originals were in order 47 | evec = temp_evec(:, 1:N); 48 | return 49 | else 50 | % Need to reorder the eigenvectors 51 | for i=1:N 52 | evec(:,i) = temp_evec(:,perm(i)); 53 | end 54 | end 55 | end -------------------------------------------------------------------------------- /errbayes.m: -------------------------------------------------------------------------------- 1 | function [e, edata, eprior] = errbayes(net, edata) 2 | %ERRBAYES Evaluate Bayesian error function for network. 3 | % 4 | % Description 5 | % E = ERRBAYES(NET, EDATA) takes a network data structure NET together 6 | % the data contribution to the error for a set of inputs and targets. 7 | % It returns the regularised error using any zero mean Gaussian priors 8 | % on the weights defined in NET. 9 | % 10 | % [E, EDATA, EPRIOR] = ERRBAYES(NET, X, T) additionally returns the 11 | % data and prior components of the error. 12 | % 13 | % See also 14 | % GLMERR, MLPERR, RBFERR 15 | % 16 | 17 | % Copyright (c) Ian T Nabney (1996-2001) 18 | 19 | % Evaluate the data contribution to the error. 20 | if isfield(net, 'beta') 21 | e1 = net.beta*edata; 22 | else 23 | e1 = edata; 24 | end 25 | 26 | % Evaluate the prior contribution to the error. 27 | if isfield(net, 'alpha') 28 | w = netpak(net); 29 | if size(net.alpha) == [1 1] 30 | eprior = 0.5*(w*w'); 31 | e2 = eprior*net.alpha; 32 | else 33 | if (isfield(net, 'mask')) 34 | nindx_cols = size(net.index, 2); 35 | nmask_rows = size(find(net.mask), 1); 36 | index = reshape(net.index(logical(repmat(net.mask, ... 37 | 1, nindx_cols))), nmask_rows, nindx_cols); 38 | else 39 | index = net.index; 40 | end 41 | eprior = 0.5*(w.^2)*index; 42 | e2 = eprior*net.alpha; 43 | end 44 | else 45 | eprior = 0; 46 | e2 = 0; 47 | end 48 | 49 | e = e1 + e2; 50 | -------------------------------------------------------------------------------- /foptions.m: -------------------------------------------------------------------------------- 1 | function opt_vect = foptions() 2 | % FOPTIONS Sets default parameters for optimisation routines 3 | % For compatibility with MATLAB's foptions() 4 | % 5 | % Copyright (c) Dharmesh Maniyar, Ian T. Nabney (2004) 6 | 7 | opt_vect = zeros(1, 18); 8 | opt_vect(2:3) = 1e-4; 9 | opt_vect(4) = 1e-6; 10 | opt_vect(16) = 1e-8; 11 | opt_vect(17) = 0.1; -------------------------------------------------------------------------------- /gauss.m: -------------------------------------------------------------------------------- 1 | function y = gauss(mu, covar, x) 2 | %GAUSS Evaluate a Gaussian distribution. 3 | % 4 | % Description 5 | % 6 | % Y = GAUSS(MU, COVAR, X) evaluates a multi-variate Gaussian density 7 | % in D-dimensions at a set of points given by the rows of the matrix X. 8 | % The Gaussian density has mean vector MU and covariance matrix COVAR. 9 | % 10 | % See also 11 | % GSAMP, DEMGAUSS 12 | % 13 | 14 | % Copyright (c) Ian T Nabney (1996-2001) 15 | 16 | [n, d] = size(x); 17 | 18 | [j, k] = size(covar); 19 | 20 | % Check that the covariance matrix is the correct dimension 21 | if ((j ~= d) | (k ~=d)) 22 | error('Dimension of the covariance matrix and data should match'); 23 | end 24 | 25 | invcov = inv(covar); 26 | mu = reshape(mu, 1, d); % Ensure that mu is a row vector 27 | 28 | x = x - ones(n, 1)*mu; 29 | fact = sum(((x*invcov).*x), 2); 30 | 31 | y = exp(-0.5*fact); 32 | 33 | y = y./sqrt((2*pi)^d*det(covar)); 34 | -------------------------------------------------------------------------------- /glmderiv.m: -------------------------------------------------------------------------------- 1 | function g = glmderiv(net, x) 2 | %GLMDERIV Evaluate derivatives of GLM outputs with respect to weights. 3 | % 4 | % Description 5 | % G = GLMDERIV(NET, X) takes a network data structure NET and a matrix 6 | % of input vectors X and returns a three-index matrix mat{g} whose I, 7 | % J, K element contains the derivative of network output K with respect 8 | % to weight or bias parameter J for input pattern I. The ordering of 9 | % the weight and bias parameters is defined by GLMUNPAK. 10 | % 11 | 12 | % Copyright (c) Ian T Nabney (1996-2001) 13 | 14 | % Check arguments for consistency 15 | errstring = consist(net, 'glm', x); 16 | if ~isempty(errstring) 17 | error(errstring); 18 | end 19 | 20 | ndata = size(x, 1); 21 | if isfield(net, 'mask') 22 | nwts = size(find(net.mask), 1); 23 | mask_array = logical(net.mask)*ones(1, net.nout); 24 | else 25 | nwts = net.nwts; 26 | end 27 | g = zeros(ndata, nwts, net.nout); 28 | 29 | temp = zeros(net.nwts, net.nout); 30 | for n = 1:ndata 31 | % Weight matrix w1 32 | temp(1:(net.nin*net.nout), :) = kron(eye(net.nout), (x(n, :))'); 33 | % Bias term b1 34 | temp(net.nin*net.nout+1:end, :) = eye(net.nout); 35 | if isfield(net, 'mask') 36 | g(n, :, :) = reshape(temp(find(mask_array)), nwts, net.nout); 37 | else 38 | g(n, :, :) = temp; 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /glmevfwd.m: -------------------------------------------------------------------------------- 1 | function [y, extra, invhess] = glmevfwd(net, x, t, x_test, invhess) 2 | %GLMEVFWD Forward propagation with evidence for GLM 3 | % 4 | % Description 5 | % Y = GLMEVFWD(NET, X, T, X_TEST) takes a network data structure NET 6 | % together with the input X and target T training data and input test 7 | % data X_TEST. It returns the normal forward propagation through the 8 | % network Y together with a matrix EXTRA which consists of error bars 9 | % (variance) for a regression problem or moderated outputs for a 10 | % classification problem. 11 | % 12 | % The optional argument (and return value) INVHESS is the inverse of 13 | % the network Hessian computed on the training data inputs and targets. 14 | % Passing it in avoids recomputing it, which can be a significant 15 | % saving for large training sets. 16 | % 17 | % See also 18 | % FEVBAYES 19 | % 20 | 21 | % Copyright (c) Ian T Nabney (1996-2001) 22 | 23 | [y, a] = glmfwd(net, x_test); 24 | if nargin == 4 25 | [extra, invhess] = fevbayes(net, y, a, x, t, x_test); 26 | else 27 | [extra, invhess] = fevbayes(net, y, a, x, t, x_test, invhess); 28 | end 29 | -------------------------------------------------------------------------------- /glmgrad.m: -------------------------------------------------------------------------------- 1 | function [g, gdata, gprior] = glmgrad(net, x, t) 2 | %GLMGRAD Evaluate gradient of error function for generalized linear model. 3 | % 4 | % Description 5 | % G = GLMGRAD(NET, X, T) takes a generalized linear model data 6 | % structure NET together with a matrix X of input vectors and a matrix 7 | % T of target vectors, and evaluates the gradient G of the error 8 | % function with respect to the network weights. The error function 9 | % corresponds to the choice of output unit activation function. Each 10 | % row of X corresponds to one input vector and each row of T 11 | % corresponds to one target vector. 12 | % 13 | % [G, GDATA, GPRIOR] = GLMGRAD(NET, X, T) also returns separately the 14 | % data and prior contributions to the gradient. 15 | % 16 | % See also 17 | % GLM, GLMPAK, GLMUNPAK, GLMFWD, GLMERR, GLMTRAIN 18 | % 19 | 20 | % Copyright (c) Ian T Nabney (1996-2001) 21 | 22 | % Check arguments for consistency 23 | errstring = consist(net, 'glm', x, t); 24 | if ~isempty(errstring); 25 | error(errstring); 26 | end 27 | 28 | y = glmfwd(net, x); 29 | delout = y - t; 30 | 31 | gw1 = x'*delout; 32 | gb1 = sum(delout, 1); 33 | 34 | gdata = [gw1(:)', gb1]; 35 | 36 | [g, gdata, gprior] = gbayes(net, gdata); 37 | -------------------------------------------------------------------------------- /glminit.m: -------------------------------------------------------------------------------- 1 | function net = glminit(net, prior) 2 | %GLMINIT Initialise the weights in a generalized linear model. 3 | % 4 | % Description 5 | % 6 | % NET = GLMINIT(NET, PRIOR) takes a generalized linear model NET and 7 | % sets the weights and biases by sampling from a Gaussian distribution. 8 | % If PRIOR is a scalar, then all of the parameters (weights and biases) 9 | % are sampled from a single isotropic Gaussian with inverse variance 10 | % equal to PRIOR. If PRIOR is a data structure similar to that in 11 | % MLPPRIOR but for a single layer of weights, then the parameters are 12 | % sampled from multiple Gaussians according to their groupings (defined 13 | % by the INDEX field) with corresponding variances (defined by the 14 | % ALPHA field). 15 | % 16 | % See also 17 | % GLM, GLMPAK, GLMUNPAK, MLPINIT, MLPPRIOR 18 | % 19 | 20 | % Copyright (c) Ian T Nabney (1996-2001) 21 | 22 | errstring = consist(net, 'glm'); 23 | if ~isempty(errstring); 24 | error(errstring); 25 | end 26 | if isstruct(prior) 27 | sig = 1./sqrt(prior.index*prior.alpha); 28 | w = sig'.*randn(1, net.nwts); 29 | elseif size(prior) == [1 1] 30 | w = randn(1, net.nwts).*sqrt(1/prior); 31 | else 32 | error('prior must be a scalar or a structure'); 33 | end 34 | 35 | net = glmunpak(net, w); 36 | 37 | -------------------------------------------------------------------------------- /glmpak.m: -------------------------------------------------------------------------------- 1 | function w = glmpak(net) 2 | %GLMPAK Combines weights and biases into one weights vector. 3 | % 4 | % Description 5 | % W = GLMPAK(NET) takes a network data structure NET and combines them 6 | % into a single row vector W. 7 | % 8 | % See also 9 | % GLM, GLMUNPAK, GLMFWD, GLMERR, GLMGRAD 10 | % 11 | 12 | % Copyright (c) Ian T Nabney (1996-2001) 13 | 14 | errstring = consist(net, 'glm'); 15 | if ~errstring 16 | error(errstring); 17 | end 18 | 19 | w = [net.w1(:)', net.b1]; 20 | 21 | -------------------------------------------------------------------------------- /glmunpak.m: -------------------------------------------------------------------------------- 1 | function net = glmunpak(net, w) 2 | %GLMUNPAK Separates weights vector into weight and bias matrices. 3 | % 4 | % Description 5 | % NET = GLMUNPAK(NET, W) takes a glm network data structure NET and a 6 | % weight vector W, and returns a network data structure identical to 7 | % the input network, except that the first-layer weight matrix W1 and 8 | % the first-layer bias vector B1 have been set to the corresponding 9 | % elements of W. 10 | % 11 | % See also 12 | % GLM, GLMPAK, GLMFWD, GLMERR, GLMGRAD 13 | % 14 | 15 | % Copyright (c) Ian T Nabney (1996-2001) 16 | 17 | % Check arguments for consistency 18 | errstring = consist(net, 'glm'); 19 | if ~errstring 20 | error(errstring); 21 | end 22 | 23 | if net.nwts ~= length(w) 24 | error('Invalid weight vector length') 25 | end 26 | 27 | nin = net.nin; 28 | nout = net.nout; 29 | net.w1 = reshape(w(1:nin*nout), nin, nout); 30 | net.b1 = reshape(w(nin*nout + 1: (nin + 1)*nout), 1, nout); 31 | -------------------------------------------------------------------------------- /gmmpak.m: -------------------------------------------------------------------------------- 1 | function p = gmmpak(mix) 2 | %GMMPAK Combines all the parameters in a Gaussian mixture model into one vector. 3 | % 4 | % Description 5 | % P = GMMPAK(NET) takes a mixture data structure MIX and combines the 6 | % component parameter matrices into a single row vector P. 7 | % 8 | % See also 9 | % GMM, GMMUNPAK 10 | % 11 | 12 | % Copyright (c) Ian T Nabney (1996-2001) 13 | 14 | errstring = consist(mix, 'gmm'); 15 | if ~errstring 16 | error(errstring); 17 | end 18 | 19 | p = [mix.priors, mix.centres(:)', mix.covars(:)']; 20 | if strcmp(mix.covar_type, 'ppca') 21 | p = [p, mix.lambda(:)', mix.U(:)']; 22 | end -------------------------------------------------------------------------------- /gmmpost.m: -------------------------------------------------------------------------------- 1 | function [post, a] = gmmpost(mix, x) 2 | %GMMPOST Computes the class posterior probabilities of a Gaussian mixture model. 3 | % 4 | % Description 5 | % This function computes the posteriors POST (i.e. the probability of 6 | % each component conditioned on the data P(J|X)) for a Gaussian mixture 7 | % model. The data structure MIX defines the mixture model, while the 8 | % matrix X contains the data vectors. Each row of X represents a 9 | % single vector. 10 | % 11 | % See also 12 | % GMM, GMMACTIV, GMMPROB 13 | % 14 | 15 | % Copyright (c) Ian T Nabney (1996-2001) 16 | 17 | % Check that inputs are consistent 18 | errstring = consist(mix, 'gmm', x); 19 | if ~isempty(errstring) 20 | error(errstring); 21 | end 22 | 23 | ndata = size(x, 1); 24 | 25 | a = gmmactiv(mix, x); 26 | 27 | post = (ones(ndata, 1)*mix.priors).*a; 28 | s = sum(post, 2); 29 | if any(s==0) 30 | warning('Some zero posterior probabilities') 31 | % Set any zeros to one before dividing 32 | zero_rows = find(s==0); 33 | s = s + (s==0); 34 | post(zero_rows, :) = 1/mix.ncentres; 35 | end 36 | post = post./(s*ones(1, mix.ncentres)); 37 | -------------------------------------------------------------------------------- /gmmprob.m: -------------------------------------------------------------------------------- 1 | function prob = gmmprob(mix, x) 2 | %GMMPROB Computes the data probability for a Gaussian mixture model. 3 | % 4 | % Description 5 | % This function computes the unconditional data density P(X) for a 6 | % Gaussian mixture model. The data structure MIX defines the mixture 7 | % model, while the matrix X contains the data vectors. Each row of X 8 | % represents a single vector. 9 | % 10 | % See also 11 | % GMM, GMMPOST, GMMACTIV 12 | % 13 | 14 | % Copyright (c) Ian T Nabney (1996-2001) 15 | 16 | % Check that inputs are consistent 17 | errstring = consist(mix, 'gmm', x); 18 | if ~isempty(errstring) 19 | error(errstring); 20 | end 21 | 22 | % Compute activations 23 | a = gmmactiv(mix, x); 24 | 25 | % Form dot product with priors 26 | prob = a * (mix.priors)'; -------------------------------------------------------------------------------- /gpcovar.m: -------------------------------------------------------------------------------- 1 | function [cov, covf] = gpcovar(net, x) 2 | %GPCOVAR Calculate the covariance for a Gaussian Process. 3 | % 4 | % Description 5 | % 6 | % COV = GPCOVAR(NET, X) takes a Gaussian Process data structure NET 7 | % together with a matrix X of input vectors, and computes the 8 | % covariance matrix COV. The inverse of this matrix is used when 9 | % calculating the mean and variance of the predictions made by NET. 10 | % 11 | % [COV, COVF] = GPCOVAR(NET, X) also generates the covariance matrix 12 | % due to the covariance function specified by NET.COVARFN as calculated 13 | % by GPCOVARF. 14 | % 15 | % See also 16 | % GP, GPPAK, GPUNPAK, GPCOVARP, GPCOVARF, GPFWD, GPERR, GPGRAD 17 | % 18 | 19 | % Copyright (c) Ian T Nabney (1996-2001) 20 | 21 | % Check arguments for consistency 22 | errstring = consist(net, 'gp', x); 23 | if ~isempty(errstring); 24 | error(errstring); 25 | end 26 | 27 | ndata = size(x, 1); 28 | 29 | % Compute prior covariance 30 | if nargout >= 2 31 | [covp, covf] = gpcovarp(net, x, x); 32 | else 33 | covp = gpcovarp(net, x, x); 34 | end 35 | 36 | % Add output noise variance 37 | cov = covp + (net.min_noise + exp(net.noise))*eye(ndata); 38 | 39 | -------------------------------------------------------------------------------- /gpcovarf.m: -------------------------------------------------------------------------------- 1 | function covf = gpcovarf(net, x1, x2) 2 | %GPCOVARF Calculate the covariance function for a Gaussian Process. 3 | % 4 | % Description 5 | % 6 | % COVF = GPCOVARF(NET, X1, X2) takes a Gaussian Process data structure 7 | % NET together with two matrices X1 and X2 of input vectors, and 8 | % computes the matrix of the covariance function values COVF. 9 | % 10 | % See also 11 | % GP, GPCOVAR, GPCOVARP, GPERR, GPGRAD 12 | % 13 | 14 | % Copyright (c) Ian T Nabney (1996-2001) 15 | 16 | errstring = consist(net, 'gp', x1); 17 | if ~isempty(errstring); 18 | error(errstring); 19 | end 20 | 21 | if size(x1, 2) ~= size(x2, 2) 22 | error('Number of variables in x1 and x2 must be the same'); 23 | end 24 | 25 | n1 = size(x1, 1); 26 | n2 = size(x2, 1); 27 | beta = diag(exp(net.inweights)); 28 | 29 | % Compute the weighted squared distances between x1 and x2 30 | z = (x1.*x1)*beta*ones(net.nin, n2) - 2*x1*beta*x2' ... 31 | + ones(n1, net.nin)*beta*(x2.*x2)'; 32 | 33 | switch net.covar_fn 34 | 35 | case 'sqexp' % Squared exponential 36 | covf = exp(net.fpar(1) - 0.5*z); 37 | 38 | case 'ratquad' % Rational quadratic 39 | nu = exp(net.fpar(2)); 40 | covf = exp(net.fpar(1))*((ones(size(z)) + z).^(-nu)); 41 | 42 | otherwise 43 | error(['Unknown covariance function ', net.covar_fn]); 44 | end -------------------------------------------------------------------------------- /gpcovarp.m: -------------------------------------------------------------------------------- 1 | function [covp, covf] = gpcovarp(net, x1, x2) 2 | %GPCOVARP Calculate the prior covariance for a Gaussian Process. 3 | % 4 | % Description 5 | % 6 | % COVP = GPCOVARP(NET, X1, X2) takes a Gaussian Process data structure 7 | % NET together with two matrices X1 and X2 of input vectors, and 8 | % computes the matrix of the prior covariance. This is the function 9 | % component of the covariance plus the exponential of the bias term. 10 | % 11 | % [COVP, COVF] = GPCOVARP(NET, X1, X2) also returns the function 12 | % component of the covariance. 13 | % 14 | % See also 15 | % GP, GPCOVAR, GPCOVARF, GPERR, GPGRAD 16 | % 17 | 18 | % Copyright (c) Ian T Nabney (1996-2001) 19 | 20 | errstring = consist(net, 'gp', x1); 21 | if ~isempty(errstring); 22 | error(errstring); 23 | end 24 | 25 | if size(x1, 2) ~= size(x2, 2) 26 | error('Number of variables in x1 and x2 must be the same'); 27 | end 28 | 29 | covf = gpcovarf(net, x1, x2); 30 | covp = covf + exp(net.bias); -------------------------------------------------------------------------------- /gpinit.m: -------------------------------------------------------------------------------- 1 | function net = gpinit(net, tr_in, tr_targets, prior) 2 | %GPINIT Initialise Gaussian Process model. 3 | % 4 | % Description 5 | % NET = GPINIT(NET, TRIN, TRTARGETS) takes a Gaussian Process data 6 | % structure NET together with a matrix TRIN of training input vectors 7 | % and a matrix TRTARGETS of training target vectors, and stores them 8 | % in NET. These datasets are required if the corresponding inverse 9 | % covariance matrix is not supplied to GPFWD. This is important if the 10 | % data structure is saved and then reloaded before calling GPFWD. Each 11 | % row of TRIN corresponds to one input vector and each row of TRTARGETS 12 | % corresponds to one target vector. 13 | % 14 | % NET = GPINIT(NET, TRIN, TRTARGETS, PRIOR) additionally initialises 15 | % the parameters in NET from the PRIOR data structure which contains 16 | % the mean and variance of the Gaussian distribution which is sampled 17 | % from. 18 | % 19 | % See also 20 | % GP, GPFWD 21 | % 22 | 23 | % Copyright (c) Ian T Nabney (1996-2001) 24 | 25 | errstring = consist(net, 'gp', tr_in, tr_targets); 26 | if ~isempty(errstring); 27 | error(errstring); 28 | end 29 | 30 | if nargin >= 4 31 | % Initialise weights at random 32 | if size(prior.pr_mean) == [1 1] 33 | w = randn(1, net.nwts).*sqrt(prior.pr_var) + ... 34 | repmat(prior.pr_mean, 1, net.nwts); 35 | else 36 | sig = sqrt(prior.index*prior.pr_var); 37 | w = sig'.*randn(1, net.nwts) + (prior.index*prior.pr_mean)'; 38 | end 39 | net = gpunpak(net, w); 40 | end 41 | 42 | net.tr_in = tr_in; 43 | net.tr_targets = tr_targets; 44 | -------------------------------------------------------------------------------- /gppak.m: -------------------------------------------------------------------------------- 1 | function hp = gppak(net) 2 | %GPPAK Combines GP hyperparameters into one vector. 3 | % 4 | % Description 5 | % HP = GPPAK(NET) takes a Gaussian Process data structure NET and 6 | % combines the hyperparameters into a single row vector HP. 7 | % 8 | % See also 9 | % GP, GPUNPAK, GPFWD, GPERR, GPGRAD 10 | % 11 | 12 | % Copyright (c) Ian T Nabney (1996-2001) 13 | 14 | % Check arguments for consistency 15 | errstring = consist(net, 'gp'); 16 | if ~isempty(errstring); 17 | error(errstring); 18 | end 19 | hp = [net.bias, net.noise, net.inweights, net.fpar]; 20 | -------------------------------------------------------------------------------- /gpunpak.m: -------------------------------------------------------------------------------- 1 | function net = gpunpak(net, hp) 2 | %GPUNPAK Separates hyperparameter vector into components. 3 | % 4 | % Description 5 | % NET = GPUNPAK(NET, HP) takes an Gaussian Process data structure NET 6 | % and a hyperparameter vector HP, and returns a Gaussian Process data 7 | % structure identical to the input model, except that the covariance 8 | % bias BIAS, output noise NOISE, the input weight vector INWEIGHTS and 9 | % the vector of covariance function specific parameters FPAR have all 10 | % been set to the corresponding elements of HP. 11 | % 12 | % See also 13 | % GP, GPPAK, GPFWD, GPERR, GPGRAD 14 | % 15 | 16 | % Copyright (c) Ian T Nabney (1996-2001) 17 | 18 | % Check arguments for consistency 19 | errstring = consist(net, 'gp'); 20 | if ~isempty(errstring); 21 | error(errstring); 22 | end 23 | if net.nwts ~= length(hp) 24 | error('Invalid weight vector length'); 25 | end 26 | 27 | net.bias = hp(1); 28 | net.noise = hp(2); 29 | 30 | % Unpack input weights 31 | mark1 = 2 + net.nin; 32 | net.inweights = hp(3:mark1); 33 | 34 | % Unpack function specific parameters 35 | net.fpar = hp(mark1 + 1:size(hp, 2)); 36 | 37 | -------------------------------------------------------------------------------- /gsamp.m: -------------------------------------------------------------------------------- 1 | function x = gsamp(mu, covar, nsamp) 2 | %GSAMP Sample from a Gaussian distribution. 3 | % 4 | % Description 5 | % 6 | % X = GSAMP(MU, COVAR, NSAMP) generates a sample of size NSAMP from a 7 | % D-dimensional Gaussian distribution. The Gaussian density has mean 8 | % vector MU and covariance matrix COVAR, and the matrix X has NSAMP 9 | % rows in which each row represents a D-dimensional sample vector. 10 | % 11 | % See also 12 | % GAUSS, DEMGAUSS 13 | % 14 | 15 | % Copyright (c) Ian T Nabney (1996-2001) 16 | 17 | d = size(covar, 1); 18 | 19 | mu = reshape(mu, 1, d); % Ensure that mu is a row vector 20 | 21 | [evec, eval] = eig(covar); 22 | 23 | deig=diag(eval); 24 | 25 | if (~isreal(deig)) | any(deig<0), 26 | warning('Covariance Matrix is not OK, redefined to be positive definite'); 27 | eval=abs(eval); 28 | end 29 | 30 | coeffs = randn(nsamp, d)*sqrt(eval); 31 | 32 | x = ones(nsamp, 1)*mu + coeffs*evec'; 33 | -------------------------------------------------------------------------------- /gtmfwd.m: -------------------------------------------------------------------------------- 1 | function mix = gtmfwd(net) 2 | %GTMFWD Forward propagation through GTM. 3 | % 4 | % Description 5 | % MIX = GTMFWD(NET) takes a GTM structure NET, and forward propagates 6 | % the latent data sample NET.X through the GTM to generate the 7 | % structure MIX which represents the Gaussian mixture model in data 8 | % space. 9 | % 10 | % See also 11 | % GTM 12 | % 13 | 14 | % Copyright (c) Ian T Nabney (1996-2001) 15 | 16 | net.gmmnet.centres = rbffwd(net.rbfnet, net.X); 17 | mix = net.gmmnet; 18 | -------------------------------------------------------------------------------- /gtmlmean.m: -------------------------------------------------------------------------------- 1 | function means = gtmlmean(net, data) 2 | %GTMLMEAN Mean responsibility for data in a GTM. 3 | % 4 | % Description 5 | % MEANS = GTMLMEAN(NET, DATA) takes a GTM structure NET, and computes 6 | % the means of the responsibility distributions for each data point in 7 | % DATA. 8 | % 9 | % See also 10 | % GTM, GTMPOST, GTMLMODE 11 | % 12 | 13 | % Copyright (c) Ian T Nabney (1996-2001) 14 | 15 | % Check for consistency 16 | errstring = consist(net, 'gtm', data); 17 | if ~isempty(errstring) 18 | error(errstring); 19 | end 20 | 21 | R = gtmpost(net, data); 22 | means = R*net.X; 23 | -------------------------------------------------------------------------------- /gtmlmode.m: -------------------------------------------------------------------------------- 1 | function modes = gtmlmode(net, data) 2 | %GTMLMODE Mode responsibility for data in a GTM. 3 | % 4 | % Description 5 | % MODES = GTMLMODE(NET, DATA) takes a GTM structure NET, and computes 6 | % the modes of the responsibility distributions for each data point in 7 | % DATA. These will always lie at one of the latent space sample points 8 | % NET.X. 9 | % 10 | % See also 11 | % GTM, GTMPOST, GTMLMEAN 12 | % 13 | 14 | % Copyright (c) Ian T Nabney (1996-2001) 15 | 16 | % Check for consistency 17 | errstring = consist(net, 'gtm', data); 18 | if ~isempty(errstring) 19 | error(errstring); 20 | end 21 | 22 | R = gtmpost(net, data); 23 | % Mode is maximum responsibility 24 | [max_resp, max_index] = max(R, [], 2); 25 | modes = net.X(max_index, :); 26 | -------------------------------------------------------------------------------- /gtmmag.m: -------------------------------------------------------------------------------- 1 | function mags = gtmmag(net, latent_data) 2 | %GTMMAG Magnification factors for a GTM 3 | % 4 | % Description 5 | % MAGS = GTMMAG(NET, LATENTDATA) takes a GTM structure NET, and 6 | % computes the magnification factors for each point the latent space 7 | % contained in LATENTDATA. 8 | % 9 | % See also 10 | % GTM, GTMPOST, GTMLMEAN 11 | % 12 | 13 | % Copyright (c) Ian T Nabney (1996-2001) 14 | 15 | errstring = consist(net, 'gtm'); 16 | if ~isempty(errstring) 17 | error(errstring); 18 | end 19 | 20 | Jacs = rbfjacob(net.rbfnet, latent_data); 21 | nlatent = size(latent_data, 1); 22 | mags = zeros(nlatent, 1); 23 | temp = zeros(net.rbfnet.nin, net.rbfnet.nout); 24 | for m = 1:nlatent 25 | temp = squeeze(Jacs(m, :, :)); % Turn into a 2d matrix 26 | mags(m) = sqrt(det(temp*temp')); 27 | end 28 | -------------------------------------------------------------------------------- /gtmpost.m: -------------------------------------------------------------------------------- 1 | function [post, a] = gtmpost(net, data) 2 | %GTMPOST Latent space responsibility for data in a GTM. 3 | % 4 | % Description 5 | % POST = GTMPOST(NET, DATA) takes a GTM structure NET, and computes 6 | % the responsibility at each latent space sample point NET.X for each 7 | % data point in DATA. 8 | % 9 | % [POST, A] = GTMPOST(NET, DATA) also returns the activations A of the 10 | % GMM NET.GMMNET as computed by GMMPOST. 11 | % 12 | % See also 13 | % GTM, GTMEM, GTMLMEAN, GMLMODE, GMMPROB 14 | % 15 | 16 | % Copyright (c) Ian T Nabney (1996-2001) 17 | 18 | % Check for consistency 19 | errstring = consist(net, 'gtm', data); 20 | if ~isempty(errstring) 21 | error(errstring); 22 | end 23 | 24 | net.gmmnet.centres = rbffwd(net.rbfnet, net.X); 25 | 26 | [post, a] = gmmpost(net.gmmnet, data); 27 | -------------------------------------------------------------------------------- /gtmprob.m: -------------------------------------------------------------------------------- 1 | function prob = gtmprob(net, data) 2 | %GTMPROB Probability for data under a GTM. 3 | % 4 | % Description 5 | % PROB = GTMPROB(NET, DATA) takes a GTM structure NET, and computes 6 | % the probability of each point in the dataset DATA. 7 | % 8 | % See also 9 | % GTM, GTMEM, GTMLMEAN, GTMLMODE, GTMPOST 10 | % 11 | 12 | % Copyright (c) Ian T Nabney (1996-2001) 13 | 14 | % Check for consistency 15 | errstring = consist(net, 'gtm', data); 16 | if ~isempty(errstring) 17 | error(errstring); 18 | end 19 | 20 | net.gmmnet.centres = rbffwd(net.rbfnet, net.X); 21 | 22 | prob = gmmprob(net.gmmnet, data); 23 | -------------------------------------------------------------------------------- /hbayes.m: -------------------------------------------------------------------------------- 1 | function [h, hdata] = hbayes(net, hdata) 2 | %HBAYES Evaluate Hessian of Bayesian error function for network. 3 | % 4 | % Description 5 | % H = HBAYES(NET, HDATA) takes a network data structure NET together 6 | % the data contribution to the Hessian for a set of inputs and targets. 7 | % It returns the regularised Hessian using any zero mean Gaussian 8 | % priors on the weights defined in NET. In addition, if a MASK is 9 | % defined in NET, then the entries in H that correspond to weights with 10 | % a 0 in the mask are removed. 11 | % 12 | % [H, HDATA] = HBAYES(NET, HDATA) additionally returns the data 13 | % component of the Hessian. 14 | % 15 | % See also 16 | % GBAYES, GLMHESS, MLPHESS, RBFHESS 17 | % 18 | 19 | % Copyright (c) Ian T Nabney (1996-2001) 20 | 21 | if (isfield(net, 'mask')) 22 | % Extract relevant entries in Hessian 23 | nmask_rows = size(find(net.mask), 1); 24 | hdata = reshape(hdata(logical(net.mask*(net.mask'))), ... 25 | nmask_rows, nmask_rows); 26 | nwts = nmask_rows; 27 | else 28 | nwts = net.nwts; 29 | end 30 | if isfield(net, 'beta') 31 | h = net.beta*hdata; 32 | else 33 | h = hdata; 34 | end 35 | 36 | if isfield(net, 'alpha') 37 | if size(net.alpha) == [1 1] 38 | h = h + net.alpha*eye(nwts); 39 | else 40 | if isfield(net, 'mask') 41 | nindx_cols = size(net.index, 2); 42 | index = reshape(net.index(logical(repmat(net.mask, ... 43 | 1, nindx_cols))), nmask_rows, nindx_cols); 44 | else 45 | index = net.index; 46 | end 47 | h = h + diag(index*net.alpha); 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /help/conffig.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | conffig(y, t) 20 | fh = conffig(y, t)21 | 22 | 23 |
conffig(y, t)
displays the confusion matrix
27 | and classification performance for the predictions mat{y}
28 | compared with the targets t
. The data is assumed to be in a
29 | 1-of-N encoding, unless there is just one column, when it is assumed to
30 | be a 2 class problem with a 0-1 encoding. Each row of y
and t
31 | corresponds to a single example.
32 |
33 | In the confusion matrix, the rows represent the true classes and the 34 | columns the predicted classes. 35 | 36 |
fh = conffig(y, t)
also returns the figure handle fh
which
37 | can be used, for instance, to delete the figure when it is no longer needed.
38 |
39 |
confmat
, demtrain
Copyright (c) Ian T Nabney (1996-9) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /help/confmat.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | [C, rate] = confmat(y, t)20 | 21 | 22 |
[C, rate] = confmat(y, t)
computes the confusion matrix C
26 | and classification performance rate
for the predictions mat{y}
27 | compared with the targets t
. The data is assumed to be in a
28 | 1-of-N encoding, unless there is just one column, when it is assumed to
29 | be a 2 class problem with a 0-1 encoding. Each row of y
and t
30 | corresponds to a single example.
31 |
32 | In the confusion matrix, the rows represent the true classes and the
33 | columns the predicted classes. The vector rate
has two entries:
34 | the percentage of correct classifications and the total number of
35 | correct classifications.
36 |
37 |
conffig
, demtrain
Copyright (c) Ian T Nabney (1996-9) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /help/dem2ddat.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | data = dem2ddat(ndata)20 | 21 |
22 | [data, c] = dem2ddat(ndata)23 | 24 | 25 |
data = dem2ddat(ndata)
generates ndata
32 | points.
33 |
34 | [data, c] = dem2ddat(ndata)
also returns a matrix containing the
35 | centres of the Gaussian distributions.
36 |
37 |
demgmm1
, demkmean
, demknn1
Copyright (c) Ian T Nabney (1996-9) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /help/demev1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demev120 | 21 | 22 |
x
which sampled from a
26 | Gaussian distribution, and a target variable t
generated by
27 | computing sin(2*pi*x)
and adding Gaussian noise. A 2-layer
28 | network with linear outputs is trained by minimizing a sum-of-squares
29 | error function with isotropic Gaussian regularizer, using the scaled
30 | conjugate gradient optimizer. The hyperparameters alpha
and
31 | beta
are re-estimated using the function evidence
. A graph
32 | is plotted of the original function, the training data, the trained
33 | network function, and the error bars.
34 |
35 | evidence
, mlp
, scg
, demard
, demmlp1
Copyright (c) Ian T Nabney (1996-9) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /help/demev2.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demev220 | 21 | 22 |
x
is sampled
26 | from a mixture of four Gaussians. Each class is
27 | associated with two of the Gaussians so that the optimal decision
28 | boundary is non-linear.
29 | A 2-layer
30 | network with logistic outputs is trained by minimizing the cross-entropy
31 | error function with isotroipc Gaussian regularizer (one hyperparameter for
32 | each of the four standard weight groups), using the scaled
33 | conjugate gradient optimizer. The hyperparameter vectors alpha
and
34 | beta
are re-estimated using the function evidence
. A graph
35 | is plotted of the optimal, regularised, and unregularised decision
36 | boundaries. A further plot of the moderated versus unmoderated contours
37 | is generated.
38 |
39 | evidence
, mlp
, scg
, demard
, demmlp2
Copyright (c) Ian T Nabney (1996-9) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /help/demev3.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demev320 | 21 | 22 |
x
which sampled from a
26 | Gaussian distribution, and a target variable t
generated by
27 | computing sin(2*pi*x)
and adding Gaussian noise. An RBF
28 | network with linear outputs is trained by minimizing a sum-of-squares
29 | error function with isotropic Gaussian regularizer, using the scaled
30 | conjugate gradient optimizer. The hyperparameters alpha
and
31 | beta
are re-estimated using the function evidence
. A graph
32 | is plotted of the original function, the training data, the trained
33 | network function, and the error bars.
34 |
35 | demev1
, evidence
, rbf
, scg
, netevfwd
Copyright (c) Ian T Nabney (1996-9) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /help/demgauss.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demgauss 20 |21 | 22 | 23 |
demgauss
provides a simple illustration of the generation of
28 | data from Gaussian distributions. It first samples from a
29 | one-dimensional distribution using randn
, and then plots a
30 | normalized histogram estimate of the distribution using histp
31 | together with the true density calculated using gauss
.
32 |
33 |
demgauss
then demonstrates sampling from a Gaussian distribution
34 | in two dimensions. It creates a mean vector and a covariance matrix,
35 | and then plots contours of constant density using the function
36 | gauss
. A sample of points drawn from this distribution, obtained
37 | using the function gsamp
, is then superimposed on the contours.
38 |
39 |
gauss
, gsamp
, histp
Copyright (c) Ian T Nabney (1996-9) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /help/demglm1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demglm120 | 21 | 22 |
data
and a vector of classifications t
. The data is
28 | generated from two Gaussian clusters, and a generalized linear model
29 | with logistic output is trained using iterative reweighted least squares.
30 | A plot of the data together with the 0.1, 0.5 and 0.9 contour lines
31 | of the conditional probability is generated.
32 |
33 | demglm2
, glm
, glmtrain
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/demglm2.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demglm120 | 21 | 22 |
data
and a vector of classifications t
. The data is
28 | generated from three Gaussian clusters, and a generalized linear model
29 | with softmax output is trained using iterative reweighted least squares.
30 | A plot of the data together with regions shaded by the classification
31 | given by the network is generated.
32 |
33 | demglm1
, glm
, glmtrain
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/demgmm1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demgmm120 | 21 | 22 |
demgmm2
, demgmm3
, demgmm4
, gmm
, gmmem
, gmmpost
Copyright (c) Ian T Nabney (1996-9) 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /help/demgp.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demgp20 | 21 | 22 |
x
and one target variable
26 | t
. The values in x
are chosen in two separated clusters and the
27 | target data is generated by computing sin(2*pi*x)
and adding Gaussian
28 | noise. Two Gaussian Processes, each with different covariance functions
29 | are trained by optimising the hyperparameters
30 | using the scaled conjugate gradient algorithm. The final predictions are
31 | plotted together with 2 standard deviation error bars.
32 |
33 | gp
, gperr
, gpfwd
, gpgrad
, gpinit
, scg
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/demgpard.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demgpare20 | 21 | 22 |
x1
, x2
and
26 | x3
, and one target variable
27 | t
. The
28 | target data is generated by computing sin(2*pi*x1)
and adding Gaussian
29 | noise, x2 is a copy of x1 with a higher level of added
30 | noise, and x3 is sampled randomly from a Gaussian distribution.
31 | A Gaussian Process, is
32 | trained by optimising the hyperparameters
33 | using the scaled conjugate gradient algorithm. The final values of the
34 | hyperparameters show that the model successfully identifies the importance
35 | of each input.
36 |
37 | demgp
, gp
, gperr
, gpfwd
, gpgrad
, gpinit
, scg
Copyright (c) Ian T Nabney (1996-9) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /help/demgpot.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | g = demgpot(x, mix)20 | 21 | 22 |
p(x)
with respect to the coefficients of the
27 | data vector x
for a Gaussian mixture model. The data structure
28 | mix
defines the mixture model, while the matrix x
contains
29 | the data vector as a row vector. Note the unusual order of the arguments:
30 | this is so that the function can be used in demhmc1
directly for
31 | sampling from the distribution p(x)
.
32 |
33 | demhmc1
, demmet1
, dempot
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/demgtm1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demgtm120 | 21 | 22 |
demgtm2
, gtm
, gtmem
, gtmpost
Copyright (c) Ian T Nabney (1996-9) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/demgtm2.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demgtm220 | 21 | 22 |
demgtm1
, gtm
, gtmem
, gtmpost
Copyright (c) Ian T Nabney (1996-9) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /help/demhint.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demhint 20 | demhint(nin, nhidden, nout)21 | 22 | 23 |
demhint
plots a Hinton diagram for a 2-layer feedforward network
28 | with 5 inputs, 4 hidden units and 3 outputs. The weight vector is
29 | chosen from a Gaussian distribution as described under mlp
.
30 |
31 |
demhint(nin, nhidden, nout)
allows the user to specify the
32 | number of inputs, hidden units and outputs.
33 |
34 |
hinton
, hintmat
, mlp
, mlppak
, mlpunpak
Copyright (c) Ian T Nabney (1996-9) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /help/demhmc1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demhmc120 | 21 | 22 |
demhmc3
, hmc
, dempot
, demgpot
Copyright (c) Ian T Nabney (1996-9) 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /help/demhmc2.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demhmc220 | 21 | 22 |
x
and one target variable
26 | t
with data generated by sampling x
at equal intervals and then
27 | generating target data by computing sin(2*pi*x)
and adding Gaussian
28 | noise. The model is a 2-layer network with linear outputs, and the hybrid Monte
29 | Carlo algorithm (without persistence) is used to sample from the posterior
30 | distribution of the weights. The graph shows the underlying function,
31 | 100 samples from the function given by the posterior distribution of the
32 | weights, and the average prediction (weighted by the posterior probabilities).
33 |
34 | demhmc3
, hmc
, mlp
, mlperr
, mlpgrad
Copyright (c) Ian T Nabney (1996-9) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /help/demhmc3.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demhmc320 | 21 | 22 |
x
and one target variable
26 | t
with data generated by sampling x
at equal intervals and then
27 | generating target data by computing sin(2*pi*x)
and adding Gaussian
28 | noise. The model is a 2-layer network with linear outputs, and the hybrid Monte
29 | Carlo algorithm (with persistence) is used to sample from the posterior
30 | distribution of the weights. The graph shows the underlying function,
31 | 300 samples from the function given by the posterior distribution of the
32 | weights, and the average prediction (weighted by the posterior probabilities).
33 |
34 | demhmc2
, hmc
, mlp
, mlperr
, mlpgrad
Copyright (c) Ian T Nabney (1996-9) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /help/demkmn1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demkmean20 | 21 | 22 |
demgmm1
.
31 |
32 | A cluster model with three components is trained using the batch 33 | K-means algorithm. The matrix of centres is printed after training. 34 | The second 35 | figure shows the data labelled with a colour derived from the corresponding 36 | cluster 37 | 38 |
dem2ddat
, demgmm1
, knn1
, kmeans
Copyright (c) Ian T Nabney (1996-9) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /help/demknn1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demknn120 | 21 | 22 |
demgmm1
.
31 |
32 | The second 33 | figure shows the data labelled with the corresponding class given 34 | by the classifier. 35 | 36 |
dem2ddat
, demgmm1
, knn
Copyright (c) Ian T Nabney (1996-9) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /help/demmdn1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demmdn120 | 21 | 22 |
x
and one target variable t
with data generated by
27 | sampling t
at equal intervals and then generating target data by
28 | computing t + 0.3*sin(2*pi*t)
and adding Gaussian noise. A
29 | Mixture Density Network with 3 centres in the mixture model is trained
30 | by minimizing a negative log likelihood error function using the scaled
31 | conjugate gradient optimizer.
32 |
33 | The conditional means, mixing coefficients and variances are plotted
34 | as a function of x
, and a contour plot of the full conditional
35 | density is also generated.
36 |
37 |
mdn
, mdnerr
, mdngrad
, scg
Copyright (c) Ian T Nabney (1996-9) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /help/demmet1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demmet1 20 | demmet1(plotwait)21 | 22 | 23 |
demmet1(plotwait)
allows the user to set the time (in a whole number
31 | of seconds) between the plotting of points. This is passed to pause
32 |
33 |
demhmc1
, metrop
, gmm
, dempot
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/demmlp1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demmlp120 | 21 | 22 |
x
and one target variable
26 | t
with data generated by sampling x
at equal intervals and then
27 | generating target data by computing sin(2*pi*x)
and adding Gaussian
28 | noise. A 2-layer network with linear outputs is trained by minimizing a
29 | sum-of-squares error function using the scaled conjugate gradient optimizer.
30 |
31 | mlp
, mlperr
, mlpgrad
, scg
Copyright (c) Ian T Nabney (1996-9) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/demmlp2.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demmlp220 | 21 | 22 |
mlp
, mlpfwd
, neterr
, quasinew
Copyright (c) Ian T Nabney (1996-9) 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /help/demnlab.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demnlab20 | 21 | 22 |
Copyright (c) Ian T Nabney (1996-9) 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /help/demns1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demns120 | 21 | 22 |
rbf
, rbftrain
, rbfprior
Copyright (c) Ian T Nabney (1996-9) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/demolgd1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demolgd120 | 21 | 22 |
x
and one target variable
26 | t
with data generated by sampling x
at equal intervals and then
27 | generating target data by computing sin(2*pi*x)
and adding Gaussian
28 | noise. A 2-layer network with linear outputs is trained by minimizing a
29 | sum-of-squares error function using on-line gradient descent.
30 |
31 | demmlp1
, olgd
Copyright (c) Ian T Nabney (1996-9) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/dempot.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | e = dempot(x, mix)20 | 21 | 22 |
p(x)
for a Gaussian mixture model. The data structure
27 | mix
defines the mixture model, while the matrix x
contains
28 | the data vectors.
29 |
30 | demgpot
, demhmc1
, demmet1
Copyright (c) Ian T Nabney (1996-9) 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /help/demprgp.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demprgp20 | 21 | 22 |
gp
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/demprior.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demprior20 | 21 | 22 |
aw1
, ab1
aw2
and ab2
28 | control the inverse variances of the first-layer weights, the hidden unit
29 | biases, the second-layer weights and the output unit biases respectively.
30 | Their values can be adjusted on a logarithmic scale using the sliders, or
31 | by typing values into the text boxes and pressing the return key.
32 |
33 | mlp
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/demrbf1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demrbf120 | 21 | 22 |
x
and one target variable
26 | t
with data generated by sampling x
at equal intervals and then
27 | generating target data by computing sin(2*pi*x)
and adding Gaussian
28 | noise. This data is the same as that used in demmlp1.
29 |
30 | Three different RBF networks (with different activation functions) 31 | are trained in two stages. First, a Gaussian mixture model is trained using 32 | the EM algorithm, and the centres of this model are used to set the centres 33 | of the RBF. Second, the output weights (and biases) are determined using the 34 | pseudo-inverse of the design matrix. 35 | 36 |
demmlp1
, rbf
, rbffwd
, gmm
, gmmem
Copyright (c) Ian T Nabney (1996-9) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /help/demsom1.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demsom120 | 21 | 22 |
som
, sompak
, somtrain
Copyright (c) Ian T Nabney (1996-9) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/demtrain.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | demtrain20 | 21 | 22 |
demtrain
brings up a simple GUI to show the training of
26 | an MLP network on classification and regression problems. The user
27 | should load in a dataset (which should be in Netlab format: see
28 | datread
), select the output activation function, the
29 | number of cycles and hidden units and then
30 | train the network. The scaled conjugate gradient algorithm is used.
31 | A graph shows the evolution of the error: the value is shown
32 | max(ceil(iterations / 50), 5)
cycles.
33 |
34 | Once the network is trained, it is saved to the file mlptrain.net
.
35 | The results can then be viewed as a confusion matrix (for classification
36 | problems) or a plot of output versus target (for regression problems).
37 |
38 |
confmat
, datread
, mlp
, netopt
, scg
Copyright (c) Ian T Nabney (1996-9) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /help/dist2.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | d = dist2(x, c) 20 |21 | 22 | 23 |
d = dist2(x, c)
takes two matrices of vectors and calculates the
27 | squared Euclidean distance between them. Both matrices must be of the
28 | same column dimension. If x
has m
rows and n
columns, and
29 | c
has l
rows and n
columns, then the result has
30 | m
rows and l
columns. The i, j
th entry is the
31 | squared distance from the i
th row of x
to the j
th
32 | row of c
.
33 |
34 | rbffwd
to calculate the activation of
38 | a thin plate spline function.
39 | 40 | 41 | n2 = dist2(x, c); 42 | z = log(n2.^(n2.^2)); 43 |44 | 45 | 46 |
gmmactiv
, kmeans
, rbffwd
Copyright (c) Ian T Nabney (1996-9) 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /help/eigdec.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | evals = eigdec(x, N) 20 | [evals, evec] = eigdec(x, N) 21 |22 | 23 | 24 |
evals = eigdec(x, N
computes the largest N
eigenvalues of the
29 | matrix x
in descending order. [evals, evec] = eigdec(x, N)
30 | also computes the corresponding eigenvectors.
31 |
32 | pca
, ppca
Copyright (c) Ian T Nabney (1996-9) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /help/errbayes.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | e = errbayes(net, edata) 20 | [e, edata, eprior] = errbayes(net, edata) 21 |22 | 23 | 24 |
e = errbayes(net, edata)
takes a network data structure
28 | net
together
29 | the data contribution to the error for a set of inputs and targets.
30 | It returns the regularised error using any zero mean Gaussian priors
31 | on the weights defined in
32 | net
.
33 |
34 | [e, edata, eprior] = errbayes(net, x, t)
additionally returns the
35 | data and prior components of the error.
36 |
37 |
glmerr
, mlperr
, rbferr
Copyright (c) Ian T Nabney (1996-9) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /help/gauss.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | y = gauss(mu, covar, x) 20 |21 | 22 | 23 |
y = gauss(mu, covar, x)
evaluates a multi-variate Gaussian
28 | density in d
-dimensions at a set of points given by the rows
29 | of the matrix x
. The Gaussian density has mean vector mu
30 | and covariance matrix covar
.
31 |
32 |
gsamp
, demgauss
Copyright (c) Ian T Nabney (1996-9) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /help/gbayes.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | g = gbayes(net, gdata) 20 | [g, gdata, gprior] = gbayes(net, gdata) 21 |22 | 23 | 24 |
g = gbayes(net, gdata)
takes a network data structure net
together
28 | the data contribution to the error gradient
29 | for a set of inputs and targets.
30 | It returns the regularised error gradient using any zero mean Gaussian priors
31 | on the weights defined in
32 | net
. In addition, if a mask
is defined in net
, then
33 | the entries in g
that correspond to weights with a 0 in the
34 | mask are removed.
35 |
36 | [g, gdata, gprior] = gbayes(net, gdata)
additionally returns the
37 | data and prior components of the error.
38 |
39 |
errbayes
, glmgrad
, mlpgrad
, rbfgrad
Copyright (c) Ian T Nabney (1996-9) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /help/glmderiv.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | 20 | g = glmderiv(net, x) 21 |22 | 23 | 24 |
g = glmderiv(net, x)
takes a network data structure net
and a matrix
28 | of input vectors x
and returns a three-index matrix mat{g} whose
29 | i
, j
, k
30 | element contains the derivative of network output k
with respect to
31 | weight or bias parameter j
for input pattern i
. The ordering of the
32 | weight and bias parameters is defined by glmunpak
.
33 |
34 | 38 | glm, glmunpak, glmgrad39 | 40 | 41 |
Copyright (c) Ian T Nabney (1996-9) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /help/glmevfwd.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | 20 | [y, extra] = glmevfwd(net, x, t, x_test) 21 | [y, extra, invhess] = glmevfwd(net, x, t, x_test, invhess) 22 |23 | 24 | 25 |
y = glmevfwd(net, x, t, x_test)
takes a network data structure
29 | net
together with the input x
and target t
training data
30 | and input test data x_test
.
31 | It returns the normal forward propagation through the network y
32 | together with a matrix extra
which consists of error bars (variance)
33 | for a regression problem or moderated outputs for a classification problem.
34 |
35 | The optional argument (and return value)
36 | invhess
is the inverse of the network Hessian
37 | computed on the training data inputs and targets. Passing it in avoids
38 | recomputing it, which can be a significant saving for large training sets.
39 |
40 |
fevbayes
Copyright (c) Ian T Nabney (1996-9) 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /help/glmfwd.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | y = glmfwd(net, x) 20 | [y, a] = glmfwd(net, x) 21 |22 | 23 | 24 |
y = glmfwd(net, x)
takes a generalized linear model
28 | data structure net
together with
29 | a matrix x
of input vectors, and forward propagates the inputs
30 | through the network to generate a matrix y
of output
31 | vectors. Each row of x
corresponds to one input vector and each
32 | row of y
corresponds to one output vector.
33 |
34 | [y, a] = glmfwd(net, x)
also returns a matrix a
35 | giving the summed inputs to each output unit, where each row
36 | corresponds to one pattern.
37 |
38 |
glm
, glmpak
, glmunpak
, glmerr
, glmgrad
Copyright (c) Ian T Nabney (1996-9) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /help/glmgrad.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | 20 | g = glmgrad(net, x, t) 21 | [g, gdata, gprior] = glmgrad(net, x, t) 22 |23 | 24 | 25 |
g = glmgrad(net, x, t)
takes a generalized linear model
29 | data structure net
30 | together with a matrix x
of input vectors and a matrix t
31 | of target vectors, and evaluates the gradient g
of the error
32 | function with respect to the network weights. The error function
33 | corresponds to the choice of output unit activation function. Each row
34 | of x
corresponds to one input vector and each row of t
35 | corresponds to one target vector.
36 |
37 | [g, gdata, gprior] = glmgrad(net, x, t)
also returns separately
38 | the data and prior contributions to the gradient.
39 |
40 |
glm
, glmpak
, glmunpak
, glmfwd
, glmerr
, glmtrain
Copyright (c) Ian T Nabney (1996-9) 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /help/glminit.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = glminit(net, prior) 20 |21 | 22 | 23 |
net = glminit(net, prior)
takes a generalized linear model
28 | net
and sets the weights and biases by sampling from a Gaussian
29 | distribution. If prior
is a scalar, then all of the parameters
30 | (weights and biases) are sampled from a single isotropic Gaussian with
31 | inverse variance equal to prior
. If prior
is a data
32 | structure similar to that in mlpprior
but for a single layer of
33 | weights, then the parameters
34 | are sampled from multiple Gaussians according to their groupings
35 | (defined by the index
field) with corresponding variances
36 | (defined by the alpha
field).
37 |
38 |
glm
, glmpak
, glmunpak
, mlpinit
, mlpprior
Copyright (c) Ian T Nabney (1996-9) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /help/glmpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | w = glmpak(net) 20 |21 | 22 | 23 |
w = glmpak(net)
takes a network data structure net
and
27 | combines them into a single row vector w
.
28 |
29 | glm
, glmunpak
, glmfwd
, glmerr
, glmgrad
Copyright (c) Ian T Nabney (1996-9) 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /help/glmunpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = glmunpak(net, w) 20 |21 | 22 | 23 |
net = glmunpak(net, w)
takes a glm network data structure net
and
27 | a weight vector w
, and returns a network data structure identical to
28 | the input network, except that the first-layer weight matrix
29 | w1
and the first-layer bias vector b1
have
30 | been set to the corresponding elements of w
.
31 |
32 | glm
, glmpak
, glmfwd
, glmerr
, glmgrad
Copyright (c) Ian T Nabney (1996-9) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /help/gmmactiv.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | 20 | a = gmmactiv(mix, x) 21 |22 | 23 | 24 |
a
(i.e. the
28 | probability p(x|j)
of the data conditioned on each component density)
29 | for a Gaussian mixture model. For the PPCA model, each activation
30 | is the conditional probability of x
given that it is generated
31 | by the component subspace.
32 | The data structure mix
defines the mixture model, while the matrix
33 | x
contains the data vectors. Each row of x
represents a single
34 | vector.
35 |
36 | gmm
, gmmpost
, gmmprob
Copyright (c) Ian T Nabney (1996-9) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /help/gmmpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | p = gmmpak(mix) 20 |21 | 22 | 23 |
p = gmmpak(net)
takes a mixture data structure mix
27 | and combines the component parameter matrices into a single row
28 | vector p
.
29 |
30 | gmm
, gmmunpak
Copyright (c) Ian T Nabney (1996-9) 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /help/gmmpost.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | 20 | function post = gmmpost(mix, x) 21 |22 | 23 | 24 |
post
(i.e. the probability of each
28 | component conditioned on the data p(j|x)
) for a Gaussian mixture model.
29 | The data structure mix
defines the mixture model, while the matrix
30 | x
contains the data vectors. Each row of x
represents a single
31 | vector.
32 |
33 | gmm
, gmmactiv
, gmmprob
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/gmmprob.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | 20 | function prob = gmmprob(mix, x) 21 |22 | 23 | 24 |
p(x)
for a Gaussian mixture model. The data structure
30 | mix
defines the mixture model, while the matrix x
contains
31 | the data vectors. Each row of x
represents a single vector.
32 |
33 | gmm
, gmmpost
, gmmactiv
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/gmmsamp.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | data = gmmsamp(mix, n) 20 | [data, label] = gmmsamp(mix, n) 21 |22 | 23 | 24 |
data = gsamp(mix, n)
generates a sample of size n
from a
29 | Gaussian mixture distribution defined by the mix
data
30 | structure. The matrix x
has n
31 | rows in which each row represents a mix.nin
-dimensional sample vector.
32 |
33 |
[data, label] = gmmsamp(mix, n)
also returns a column vector of
34 | classes (as an index 1..N) label
.
35 |
36 |
gsamp
, gmm
Copyright (c) Ian T Nabney (1996-9) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /help/gmmunpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | mix = gmmunpak(mix, p) 20 |21 | 22 | 23 |
mix = gmmunpak(mix, p)
27 | takes a GMM data structure mix
and
28 | a single row vector of parameters p
and returns a mixture data structure
29 | identical to the input mix
, except that the mixing coefficients
30 | priors
, centres centres
and covariances covars
31 | (and, for PPCA, the lambdas and U (PCA sub-spaces)) are all set
32 | to the corresponding elements of p
.
33 |
34 | gmm
, gmmpak
Copyright (c) Ian T Nabney (1996-9) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /help/gpcovarf.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | covf = gpcovarf(net, x1, x2) 20 |21 | 22 | 23 |
covf = gpcovarf(net, x1, x2)
takes
28 | a Gaussian Process data structure net
together with
29 | two matrices x1
and x2
of input vectors,
30 | and computes the matrix of the covariance function values
31 | covf
.
32 |
33 |
gp
, gpcovar
, gpcovarp
, gperr
, gpgrad
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/gpcovarp.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | covp = gpcovarp(net, x1, x2) 20 | [covp, covf] = gpcovarp(net, x1, x2) 21 |22 | 23 | 24 |
covp = gpcovarp(net, x1, x2)
takes
29 | a Gaussian Process data structure net
together with
30 | two matrices x1
and x2
of input vectors,
31 | and computes the matrix of the prior covariance. This is
32 | the function component of the covariance plus the exponential of the bias
33 | term.
34 |
35 |
[covp, covf] = gpcovarp(net, x1, x2)
also returns the function
36 | component of the covariance.
37 |
38 |
gp
, gpcovar
, gpcovarf
, gperr
, gpgrad
Copyright (c) Ian T Nabney (1996-9) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /help/gperr.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | edata = gperr(net, x, t) 20 | [e, edata, eprior] = gperr(net, x, t) 21 |22 | 23 | 24 |
e = gperr(net, x, t)
takes a Gaussian Process data structure net
together
28 | with a matrix x
of input vectors and a matrix t
of target
29 | vectors, and evaluates the error function e
. Each row
30 | of x
corresponds to one input vector and each row of t
31 | corresponds to one target vector.
32 |
33 | [e, edata, eprior] = gperr(net, x, t)
additionally returns the
34 | data and hyperprior components of the error, assuming a Gaussian
35 | prior on the weights with mean and variance parameters prmean
and
36 | prvariance
taken from the network data structure net
.
37 |
38 |
gp
, gpcovar
, gpfwd
, gpgrad
Copyright (c) Ian T Nabney (1996-9) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /help/gpgrad.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | g = gpgrad(net, x, t) 20 |21 | 22 | 23 |
g = gpgrad(net, x, t)
takes a Gaussian Process data structure net
together
27 | with a matrix x
of input vectors and a matrix t
of target
28 | vectors, and evaluates the error gradient g
. Each row
29 | of x
corresponds to one input vector and each row of t
30 | corresponds to one target vector.
31 |
32 | gp
, gpcovar
, gpfwd
, gperr
Copyright (c) Ian T Nabney (1996-9) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /help/gppak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | hp = gppak(net) 20 |21 | 22 | 23 |
hp = gppak(net)
takes a Gaussian Process data structure net
and
27 | combines the hyperparameters into a single row vector hp
.
28 |
29 | gp
, gpunpak
, gpfwd
, gperr
, gpgrad
Copyright (c) Ian T Nabney (1996-9) 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /help/gpunpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = gpunpak(net, hp) 20 |21 | 22 | 23 |
net = gpunpak(net, hp)
takes an Gaussian Process data structure net
and
27 | a hyperparameter vector hp
, and returns a Gaussian Process data structure
28 | identical to
29 | the input model, except that the covariance bias
30 | bias
, output noise noise
, the input weight vector
31 | inweights
and the vector of covariance function specific parameters
32 | fpar
have all
33 | been set to the corresponding elements of hp
.
34 |
35 | gp
, gppak
, gpfwd
, gperr
, gpgrad
Copyright (c) Ian T Nabney (1996-9) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /help/gsamp.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | x = gsamp(mu, covar, nsamp) 20 |21 | 22 | 23 |
x = gsamp(mu, covar, nsamp)
generates a sample of size nsamp
28 | from a d
-dimensional Gaussian distribution. The Gaussian density
29 | has mean vector mu
and covariance matrix covar
, and the
30 | matrix x
has nsamp
rows in which each row represents a
31 | d
-dimensional sample vector.
32 |
33 |
gauss
, demgauss
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/gtmfwd.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | mix = gtmfwd(net) 20 |21 | 22 | 23 |
mix = gtmfwd(net)
takes a GTM
28 | structure net
, and forward
29 | propagates the latent data sample net.X
through the GTM to generate
30 | the structure
31 | mix
which represents the Gaussian mixture model in data space.
32 |
33 | gtm
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/gtmlmean.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | means = gtmlmean(net, data) 20 |21 | 22 | 23 |
means = gtmlmean(net, data)
takes a GTM
28 | structure net
, and computes the means of the responsibility
29 | distributions for each data point in data
.
30 |
31 | gtm
, gtmpost
, gtmlmode
Copyright (c) Ian T Nabney (1996-9) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/gtmlmode.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | modes = gtmlmode(net, data) 20 |21 | 22 | 23 |
modes = gtmlmode(net, data)
takes a GTM
28 | structure net
, and computes the modes of the responsibility
29 | distributions for each data point in data
. These will always lie
30 | at one of the latent space sample points net.X
.
31 |
32 | gtm
, gtmpost
, gtmlmean
Copyright (c) Ian T Nabney (1996-9) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /help/gtmmag.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | mags = gtmmag(net, latentdata) 20 |21 | 22 | 23 |
mags = gtmmag(net, latentdata)
takes a GTM
28 | structure net
, and computes the magnification factors
29 | for each point the latent space contained in latentdata
.
30 |
31 | gtm
, gtmpost
, gtmlmean
Copyright (c) Ian T Nabney (1996-9) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/gtmpost.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | post = gtmpost(net, data) 20 | [post, a] = gtmpost(net, data) 21 |22 | 23 | 24 |
post = gtmpost(net, data)
takes a GTM
29 | structure net
, and computes the responsibility at each latent space
30 | sample point net.X
31 | for each data point in data
.
32 |
33 | [post, a] = gtmpost(net, data)
also returns the activations
34 | a
of the GMM net.gmmnet
as computed by gmmpost
.
35 |
36 |
gtm
, gtmem
, gtmlmean
, gmlmode
, gmmprob
Copyright (c) Ian T Nabney (1996-9) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /help/gtmprob.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | prob = gtmprob(net, data) 20 |21 | 22 | 23 |
prob = gtmprob(net, data)
takes a GTM
28 | structure net
, and computes the probability of each point in the
29 | dataset data
.
30 |
31 | gtm
, gtmem
, gtmlmean
, gtmlmode
, gtmpost
Copyright (c) Ian T Nabney (1996-9) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/hbayes.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | h = hbayes(net, hdata) 20 | [h, hdata] = hbayes(net, hdata) 21 |22 | 23 | 24 |
h = hbayes(net, hdata)
takes a network data structure net
together
28 | the data contribution to the Hessian
29 | for a set of inputs and targets.
30 | It returns the regularised Hessian using any zero mean Gaussian priors
31 | on the weights defined in
32 | net
. In addition, if a mask
is defined in net
, then
33 | the entries in h
that correspond to weights with a 0 in the
34 | mask are removed.
35 |
36 | [h, hdata] = hbayes(net, hdata)
additionally returns the
37 | data component of the Hessian.
38 |
39 |
gbayes
, glmhess
, mlphess
, rbfhess
Copyright (c) Ian T Nabney (1996-9) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /help/hesschek.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | hesschek(net, x, t) 20 | h = hesschek(net, x, t)21 | 22 | 23 |
hesschek(net, x, t)
takes a network data structure net
, together
28 | with input and target data matrices x
and t
, and compares
29 | the evaluation of the Hessian matrix using the function nethess
30 | and using central differences with the function neterr
.
31 |
32 |
The optional return value h
is the Hessian computed using
33 | nethess
.
34 |
35 |
nethess
, neterr
Copyright (c) Ian T Nabney (1996-9) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /help/hintmat.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | [xvals, yvals, color] = hintmat(w)20 | 21 | 22 |
26 | [xvals, yvals, color] = hintmat(w)27 | 28 | takes a matrix
w
and
29 | returns coordinates xvals, yvals
for the patches comrising the
30 | Hinton diagram, together with a vector color
labelling the color
31 | (black or white) of the corresponding elements according to their
32 | sign.
33 |
34 | hinton
Copyright (c) Ian T Nabney (1996-9) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /help/hinton.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | hinton(w) 20 | h = hinton(w)21 | 22 | 23 |
hinton(w)
takes a matrix w
28 | and plots the Hinton diagram.
29 |
30 |
h = hinton(net)
also returns the figure handle h
31 | which can be used, for instance, to delete the
32 | figure when it is no longer needed.
33 |
34 |
To print the figure correctly in black and white, you should call
35 | set(h, 'InvertHardCopy', 'off')
before printing.
36 |
37 |
demhint
, hintmat
, mlphint
Copyright (c) Ian T Nabney (1996-9) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /help/histp.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | h = histp(x, xmin, xmax, nbins) 20 |21 | 22 | 23 |
histp(x, xmin, xmax, nbins)
takes a column vector x
28 | of data values and generates a normalized histogram plot of the
29 | distribution. The histogram has nbins
bins lying in the
30 | range xmin
to xmax
.
31 |
32 |
h = histp(...)
returns a vector of patch handles.
33 |
34 |
demgauss
Copyright (c) Ian T Nabney (1996-9) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /help/knn.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | 20 | net = knn(nin, nout, k, tr_in, tr_targets) 21 |22 | 23 | 24 |
net = knn(nin, nout, k, tr_in, tr_targets)
creates a KNN model net
28 | with input dimension nin
, output dimension nout
and k
29 | neighbours. The training data is also stored in the data structure and the
30 | targets are assumed to be using a 1-of-N coding.
31 |
32 | The fields in net
are
33 |
34 | 35 | type = 'knn' 36 | nin = number of inputs 37 | nout = number of outputs 38 | tr_in = training input data 39 | tr_targets = training target data 40 |41 | 42 | 43 |
kmeans
, knnfwd
Copyright (c) Ian T Nabney (1996-9) 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /help/linef.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |linef(lambda, fn, x, d)
calculates the value of the function
19 | fn
at the point x+lambda*d
. Here x
is a row vector
20 | and lambda
is a scalar.
21 |
22 | linef(lambda, fn, x, d, p1, p2, ...)
allows additional
23 | arguments to be passed to fn()
.
24 | This function is used for convenience in some of the optimisation routines.
25 |
26 |
linemin
.
31 |
32 | gradchek
, linemin
Copyright (c) Ian T Nabney (1996-9) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /help/mdn2gmm.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | gmmmixes = mdn2gmm(mdnmixes) 20 |21 | 22 | 23 |
gmmmixes = mdn2gmm(mdnmixes)
takes an MDN mixture data structure
27 | mdnmixes
28 | containing three matrices (for priors, centres and variances) where each
29 | row represents the corresponding parameter values for a different mixture model
30 | and creates an array of GMMs. These can then be used with the standard
31 | Netlab Gaussian mixture model functions.
32 |
33 | 37 | 38 | mdnmixes = mdnfwd(net, x); 39 | mixes = mdn2gmm(mdnmixes); 40 | p = gmmprob(mixes(1), y); 41 |42 | 43 | This creates an array GMM mixture models (one for each data point in 44 |
x
). The vector p
is then filled with the conditional
45 | probabilities of the values y
given x(1,:)
.
46 |
47 | gmm
, mdn
, mdnfwd
Copyright (c) Ian T Nabney (1996-9) 55 |
David J Evans (1998) 56 | 57 | 58 | -------------------------------------------------------------------------------- /help/mdndist2.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | n2 = mdndist2(mixparams, t) 20 |21 | 22 | 23 |
n2 = mdndist2(mixparams, t)
takes takes the centres of the Gaussian
27 | contained in
28 | mixparams
and the target data matrix, t
, and computes the squared
29 | Euclidean distance between them. If t
has m
rows and n
30 | columns, then the centres
field in
31 | the mixparams
structure should have m
rows and
32 | n*mixparams.ncentres
columns: the centres in each row relate to
33 | the corresponding row in t
.
34 | The result has m
rows and mixparams.ncentres
columns.
35 | The i, j
th entry is the
36 | squared distance from the i
th row of x
to the j
th
37 | centre in the i
th row of mixparams.centres
.
38 |
39 | mdnfwd
, mdnprob
Copyright (c) Ian T Nabney (1996-9) 47 |
David J Evans (1998) 48 | 49 | 50 | -------------------------------------------------------------------------------- /help/mdnerr.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | e = mdnerr(net, x, t) 20 |21 | 22 | 23 |
e = mdnerr(net, x, t)
takes a mixture density network data
28 | structure net
, a matrix x
of input vectors and a matrix
29 | t
of target vectors, and evaluates the error function
30 | e
. The error function is the negative log likelihood of the
31 | target data under the conditional density given by the mixture model
32 | parameterised by the MLP. Each row of x
corresponds to one
33 | input vector and each row of t
corresponds to one target vector.
34 |
35 | mdn
, mdnfwd
, mdngrad
Copyright (c) Ian T Nabney (1996-9) 43 |
David J Evans (1998) 44 | 45 | 46 | -------------------------------------------------------------------------------- /help/mdngrad.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | 20 | g = mdngrad(net, x, t) 21 |22 | 23 | 24 |
g = mdngrad(net, x, t)
takes a mixture density network data
29 | structure net
, a matrix x
of input vectors and a matrix
30 | t
of target vectors, and evaluates the gradient g
of the
31 | error function with respect to the network weights. The error function
32 | is negative log likelihood of the target data. Each row of x
33 | corresponds to one input vector and each row of t
corresponds to
34 | one target vector.
35 |
36 | mdn
, mdnfwd
, mdnerr
, mdnprob
, mlpbkp
Copyright (c) Ian T Nabney (1996-9) 44 |
David J Evans (1998) 45 | 46 | 47 | -------------------------------------------------------------------------------- /help/mdninit.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = mdninit(net, prior) 20 | net = mdninit(net, prior, t, options) 21 |22 | 23 | 24 |
net = mdninit(net, prior)
takes a Mixture Density Network
29 | net
and sets the weights and biases by sampling from a Gaussian
30 | distribution. It calls mlpinit
for the MLP component of net
.
31 |
32 |
net = mdninit(net, prior, t, options)
uses the target data t
to
33 | initialise the biases for the output units after initialising the
34 | other weights as above. It calls gmminit
, with t
and options
35 | as arguments, to obtain a model of the unconditional density of t
. The
36 | biases are then set so that net
will output the values in the Gaussian
37 | mixture model.
38 |
39 |
mdn
, mlp
, mlpinit
, gmminit
Copyright (c) Ian T Nabney (1996-9) 47 |
David J Evans (1998) 48 | 49 | 50 | -------------------------------------------------------------------------------- /help/mdnpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | w = mdnpak(net) 20 |21 | 22 | 23 |
w = mdnpak(net)
takes a mixture density
27 | network data structure net
and
28 | combines the network weights into a single row vector w
.
29 |
30 | mdn
, mdnunpak
, mdnfwd
, mdnerr
, mdngrad
Copyright (c) Ian T Nabney (1996-9) 38 |
David J Evans (1998) 39 | 40 | 41 | -------------------------------------------------------------------------------- /help/mdnpost.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | post = mdnpost(mixparams, t) 20 | [post, a] = mdnpost(mixparams, t) 21 |22 | 23 | 24 |
post = mdnpost(mixparams, t)
computes the posterior
28 | probability p(j|t)
of each
29 | data vector in t
under the Gaussian mixture model represented by the
30 | corresponding entries in mixparams
. Each row of t
represents a
31 | single vector.
32 |
33 | [post, a] = mdnpost(mixparams, t)
also computes the activations
34 | a
(i.e. the probability p(t|j)
of the data conditioned on
35 | each component density) for a Gaussian mixture model.
36 |
37 |
mdngrad
, mdnprob
Copyright (c) Ian T Nabney (1996-9) 45 |
David J Evans (1998) 46 | 47 | 48 | -------------------------------------------------------------------------------- /help/mdnprob.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | prob = mdnprob(mixparams, t) 20 | [prob, a] = mdnprob(mixparams, t) 21 |22 | 23 | 24 |
prob = mdnprob(mixparams, t)
computes the probability p(t)
of each
28 | data vector in t
under the Gaussian mixture model represented by the
29 | corresponding entries in mixparams
. Each row of t
represents a
30 | single vector.
31 |
32 | [prob, a] = mdnprob(mixparams, t)
also computes the activations
33 | a
(i.e. the probability p(t|j)
of the data conditioned on
34 | each component density) for a Gaussian mixture model.
35 |
36 |
mdnerr
, mdnpost
Copyright (c) Ian T Nabney (1996-9) 44 |
David J Evans (1998) 45 | 46 | 47 | -------------------------------------------------------------------------------- /help/mdnunpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = mdnunpak(net, w) 20 |21 | 22 | 23 |
net = mdnunpak(net, w)
takes an mdn network data structure net
and
27 | a weight vector w
, and returns a network data structure identical to
28 | the input network, except that the weights in the MLP sub-structure are
29 | set to the corresponding elements of w
.
30 |
31 | mdn
, mdnpak
, mdnfwd
, mdnerr
, mdngrad
Copyright (c) Ian T Nabney (1996-9) 39 |
David J Evans (1998) 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/mlpbkp.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | g = mlpbkp(net, x, z, deltas)20 | 21 | 22 |
g = mlpbkp(net, x, z, deltas)
takes a network data structure
26 | net
together with a matrix x
of input vectors, a matrix
27 | z
of hidden unit activations, and a matrix deltas
of the
28 | gradient of the error function with respect to the values of the
29 | output units (i.e. the summed inputs to the output units, before the
30 | activation function is applied). The return value is the gradient
31 | g
of the error function with respect to the network
32 | weights. Each row of x
corresponds to one input vector.
33 |
34 | This function is provided so that the common backpropagation algorithm 35 | can be used by multi-layer perceptron network models to compute 36 | gradients for mixture density networks as well as standard error 37 | functions. 38 | 39 |
mlp
, mlpgrad
, mlpderiv
, mdngrad
Copyright (c) Ian T Nabney (1996-9) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /help/mlpderiv.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | g = mlpderiv(net, x)20 | 21 | 22 |
g = mlpderiv(net, x)
takes a network data structure net
26 | and a matrix of input vectors x
and returns a three-index matrix
27 | g
whose i
, j
, k
element contains the
28 | derivative of network output k
with respect to weight or bias
29 | parameter j
for input pattern i
. The ordering of the
30 | weight and bias parameters is defined by mlpunpak
.
31 |
32 | mlp
, mlppak
, mlpgrad
, mlpbkp
Copyright (c) Ian T Nabney (1996-9) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /help/mlpevfwd.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | 20 | [y, extra] = mlpevfwd(net, x, t, x_test) 21 | [y, extra, invhess] = mlpevfwd(net, x, t, x_test, invhess) 22 |23 | 24 | 25 |
y = mlpevfwd(net, x, t, x_test)
takes a network data structure
29 | net
together with the input x
and target t
training data
30 | and input test data x_test
.
31 | It returns the normal forward propagation through the network y
32 | together with a matrix extra
which consists of error bars (variance)
33 | for a regression problem or moderated outputs for a classification problem.
34 | The optional argument (and return value)
35 | invhess
is the inverse of the network Hessian
36 | computed on the training data inputs and targets. Passing it in avoids
37 | recomputing it, which can be a significant saving for large training sets.
38 |
39 | fevbayes
Copyright (c) Ian T Nabney (1996-9) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /help/mlphdotv.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | hdv = mlphdotv(net, x, t, v)20 | 21 | 22 |
hdv = mlphdotv(net, x, t, v)
takes an MLP network data structure
27 | net
, together with the matrix x
of input vectors, the
28 | matrix t
of target vectors and an arbitrary row vector v
29 | whose length equals the number of parameters in the network, and
30 | returns the product of the data-dependent contribution to the Hessian
31 | matrix with v
. The implementation is based on the R-propagation
32 | algorithm of Pearlmutter.
33 |
34 |
mlp
, mlphess
, hesschek
Copyright (c) Ian T Nabney (1996-9) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /help/mlphint.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | mlphint(net) 20 | [h1, h2] = mlphint(net)21 | 22 | 23 |
mlphint(net)
takes a network structure net
28 | and plots the Hinton diagram comprised of two
29 | figure windows, one displaying the first-layer weights and biases, and
30 | one displaying the second-layer weights and biases.
31 |
32 |
[h1, h2] = mlphint(net)
also returns handles h1
and
33 | h2
to the figures which can be used, for instance, to delete the
34 | figures when they are no longer needed.
35 |
36 |
To print the figure correctly, you should call
37 | set(h, 'InvertHardCopy', 'on')
before printing.
38 |
39 |
demhint
, hintmat
, mlp
, mlppak
, mlpunpak
Copyright (c) Ian T Nabney (1996-9) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /help/mlpinit.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = mlpinit(net, prior) 20 |21 | 22 | 23 |
net = mlpinit(net, prior)
takes a 2-layer feedforward network
28 | net
and sets the weights and biases by sampling from a Gaussian
29 | distribution. If prior
is a scalar, then all of the parameters
30 | (weights and biases) are sampled from a single isotropic Gaussian with
31 | inverse variance equal to prior
. If prior
is a data
32 | structure of the kind generated by mlpprior
, then the parameters
33 | are sampled from multiple Gaussians according to their groupings
34 | (defined by the index
field) with corresponding variances
35 | (defined by the alpha
field).
36 |
37 |
mlp
, mlpprior
, mlppak
, mlpunpak
Copyright (c) Ian T Nabney (1996-9) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /help/mlptrain.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |[net, error] = mlptrain(net, x, t, its)
trains a network data
20 | structure net
using the scaled conjugate gradient algorithm
21 | for its
cycles with
22 | input data x
, target data t
.
23 |
24 |
demtrain
, scg
, netopt
Copyright (c) Ian T Nabney (1996-9) 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /help/mlpunpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = mlpunpak(net, w) 20 |21 | 22 | 23 |
net = mlpunpak(net, w)
takes an mlp network data structure net
and
27 | a weight vector w
, and returns a network data structure identical to
28 | the input network, except that the first-layer weight matrix
29 | w1
, the first-layer bias vector b1
, the second-layer
30 | weight matrix w2
and the second-layer bias vector b2
have all
31 | been set to the corresponding elements of w
.
32 |
33 | mlp
, mlppak
, mlpfwd
, mlperr
, mlpbkp
, mlpgrad
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/netderiv.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | g = netderiv(w, net, x)20 | 21 | 22 |
g = netderiv(w, net, x)
takes a weight vector w
and a network
27 | data structure net
, together with the matrix x
of input
28 | vectors, and returns the
29 | gradient of the outputs with respect to the weights evaluated at w
.
30 |
31 |
netevfwd
, netopt
Copyright (c) Ian T Nabney (1996-9) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/neterr.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | e = neterr(w, net, x, t) 20 | [e, varargout] = neterr(w, net, x, t) 21 |22 | 23 | 24 |
e = neterr(w, net, x, t)
takes a weight vector w
and a network
29 | data structure net
, together with the matrix x
of input
30 | vectors and the matrix t
of target vectors, and returns the
31 | value of the error function evaluated at w
.
32 |
33 |
[e, varargout] = neterr(w, net, x, t)
also returns any additional
34 | return values from the error function.
35 |
36 |
netgrad
, nethess
, netopt
Copyright (c) Ian T Nabney (1996-9) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /help/netgrad.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | g = netgrad(w, net, x, t)20 | 21 | 22 |
g = netgrad(w, net, x, t)
takes a weight vector w
and a network
27 | data structure net
, together with the matrix x
of input
28 | vectors and the matrix t
of target vectors, and returns the
29 | gradient of the error function evaluated at w
.
30 |
31 |
mlp
, neterr
, netopt
Copyright (c) Ian T Nabney (1996-9) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/netinit.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = netinit(net, prior) 20 |21 | 22 | 23 |
net = netinit(net, prior)
takes a network data structure
28 | net
and sets the weights and biases by sampling from a Gaussian
29 | distribution. If prior
is a scalar, then all of the parameters
30 | (weights and biases) are sampled from a single isotropic Gaussian with
31 | inverse variance equal to prior
. If prior
is a data
32 | structure of the kind generated by mlpprior
, then the parameters
33 | are sampled from multiple Gaussians according to their groupings
34 | (defined by the index
field) with corresponding variances
35 | (defined by the alpha
field).
36 |
37 |
mlpprior
, netunpak
, rbfprior
Copyright (c) Ian T Nabney (1996-9) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /help/netpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | w = netpak(net) 20 |21 | 22 | 23 |
w = netpak(net)
takes a network data structure net
and
27 | combines the component weight matrices into a single row
28 | vector w
. The facility to switch between these two
29 | representations for the network parameters is useful, for example, in
30 | training a network by error function minimization, since a single
31 | vector of parameters can be handled by general-purpose optimization
32 | routines. This function also takes into account a mask
defined
33 | as a field in net
by removing any weights that correspond to
34 | entries of 0 in the mask.
35 |
36 | net
, netunpak
, netfwd
, neterr
, netgrad
Copyright (c) Ian T Nabney (1996-9) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /help/netunpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = netunpak(net, w) 20 |21 | 22 | 23 |
net = netunpak(net, w)
takes an net network data structure net
and
27 | a weight vector w
, and returns a network data structure identical to
28 | the input network, except that the componenet weight matrices have all
29 | been set to the corresponding elements of w
. If there is
30 | a mask
field in the net
data structure, then the weights in
31 | w
are placed in locations corresponding to non-zero entries in the
32 | mask (so w
should have the same length as the number of non-zero
33 | entries in the mask
).
34 |
35 | netpak
, netfwd
, neterr
, netgrad
Copyright (c) Ian T Nabney (1996-9) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /help/pca.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | PCcoeff = pca(data) 20 | PCcoeff = pca(data, N) 21 | [PCcoeff, PCvec] = pca(data) 22 |23 | 24 | 25 |
PCcoeff = pca(data)
computes the eigenvalues of the covariance
30 | matrix of the dataset data
and returns them as PCcoeff
. These
31 | coefficients give the variance of data
along the corresponding
32 | principal components.
33 |
34 | PCcoeff = pca(data, N)
returns the largest N
eigenvalues.
35 |
36 |
[PCcoeff, PCvec] = pca(data)
returns the principal components as
37 | well as the coefficients. This is considerably more computationally
38 | demanding than just computing the eigenvalues.
39 |
40 |
eigdec
, gtminit
, ppca
Copyright (c) Ian T Nabney (1996-9) 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /help/plotmat.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | plotmat(matrix, textcolour, gridcolour, fontsize)20 | 21 | 22 |
plotmat(matrix, textcolour, gridcolour, fontsize)
displays the matrix
26 | matrix
on the current figure. The textcolour
and gridcolour
27 | arguments control the colours of the numbers and grid labels respectively and
28 | should follow the usual Matlab specification.
29 | The parameter fontsize
should be an integer.
30 |
31 | conffig
, demmlp2
Copyright (c) Ian T Nabney (1996-9) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/ppca.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | [var, U, lambda] = pca(x, ppca_dim) 20 |21 | 22 | 23 |
[var, U, lambda] = ppca(x, ppca_dim)
computes the principal component
28 | subspace U
of dimension ppca_dim
using a centred
29 | covariance matrix x
. The variable var
contains
30 | the off-subspace variance (which is assumed to be spherical), while the
31 | vector lambda
contains the variances of each of the principal
32 | components. This is computed using the eigenvalue and eigenvector
33 | decomposition of x
.
34 |
35 | eigdec
, pca
Copyright (c) Ian T Nabney (1996-9) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /help/rbfderiv.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | g = rbfderiv(net, x)20 | 21 | 22 |
g = rbfderiv(net, x)
takes a network data structure net
26 | and a matrix of input vectors x
and returns a three-index matrix
27 | g
whose i
, j
, k
element contains the
28 | derivative of network output k
with respect to weight or bias
29 | parameter j
for input pattern i
. The ordering of the
30 | weight and bias parameters is defined by rbfunpak
. This
31 | function also takes into account any mask in the network data structure.
32 |
33 | rbf
, rbfpak
, rbfgrad
, rbfbkp
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/rbfevfwd.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | 20 | [y, extra] = rbfevfwd(net, x, t, x_test) 21 | [y, extra, invhess] = rbfevfwd(net, x, t, x_test, invhess) 22 |23 | 24 | 25 |
y = rbfevfwd(net, x, t, x_test)
takes a network data structure
29 | net
together with the input x
and target t
training data
30 | and input test data x_test
.
31 | It returns the normal forward propagation through the network y
32 | together with a matrix extra
which consists of error bars (variance)
33 | for a regression problem or moderated outputs for a classification problem.
34 |
35 | The optional argument (and return value)
36 | invhess
is the inverse of the network Hessian
37 | computed on the training data inputs and targets. Passing it in avoids
38 | recomputing it, which can be a significant saving for large training sets.
39 |
40 |
fevbayes
Copyright (c) Ian T Nabney (1996-9) 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /help/rbfjacob.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | g = rbfjacob(net, x)20 | 21 | 22 |
g = rbfjacob(net, x)
takes a network data structure net
26 | and a matrix of input vectors x
and returns a three-index matrix
27 | g
whose i
, j
, k
element contains the
28 | derivative of network output k
with respect to input
29 | parameter j
for input pattern i
.
30 |
31 | rbf
, rbfgrad
, rbfbkp
Copyright (c) Ian T Nabney (1996-9) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /help/rbfpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | w = rbfpak(net) 20 |21 | 22 | 23 |
w = rbfpak(net)
takes a network data structure net
and combines
27 | the component parameter matrices into a single row vector w
.
28 |
29 | rbfunpak
, rbf
Copyright (c) Ian T Nabney (1996-9) 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /help/rbfsetbf.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = rbfsetbf(net, options, x) 20 |21 | 22 | 23 |
net = rbfsetbf(net, options, x)
sets the basis functions of the
27 | RBF network net
so that they model the unconditional density of the
28 | dataset x
. This is done by training a GMM with spherical covariances
29 | using gmmem
. The options
vector is passed to gmmem
.
30 | The widths of the functions are set by a call to rbfsetfw
.
31 |
32 | rbftrain
, rbfsetfw
, gmmem
Copyright (c) Ian T Nabney (1996-9) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /help/rbfsetfw.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = rbfsetfw(net, scale) 20 |21 | 22 | 23 |
net = rbfsetfw(net, scale)
sets the widths of
27 | the basis functions of the
28 | RBF network net
.
29 | If Gaussian basis functions are used, then the variances are set to
30 | the largest squared distance between centres if scale
is non-positive
31 | and scale
times the mean distance of each centre to its nearest
32 | neighbour if scale
is positive. Non-Gaussian basis functions do
33 | not have a width.
34 |
35 | rbftrain
, rbfsetbf
, gmmem
Copyright (c) Ian T Nabney (1996-9) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /help/rbfunpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = rbfunpak(net, w) 20 |21 | 22 | 23 |
net = rbfunpak(net, w)
takes an RBF network data structure net
and
27 | a weight vector w
, and returns a network data structure identical to
28 | the input network, except that the centres
29 | c
, the widths wi
, the second-layer
30 | weight matrix w2
and the second-layer bias vector b2
have all
31 | been set to the corresponding elements of w
.
32 |
33 | rbfpak
, rbf
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/rosegrad.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | g = rosegrad(x) 20 |21 | 22 | 23 |
g = rosegrad(x)
computes the gradient of Rosenbrock's function
27 | at each row of x
, which should have two columns.
28 |
29 | demopt1
, rosen
Copyright (c) Ian T Nabney (1996-9) 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /help/rosen.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | y = rosen(x) 20 |21 | 22 | 23 |
y = rosen(x)
computes the value of Rosenbrock's function
27 | at each row of x
, which should have two columns.
28 |
29 | demopt1
, rosegrad
Copyright (c) Ian T Nabney (1996-9) 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /help/som.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | 20 | net = som(nin, map_size) 21 |22 | 23 | 24 |
net = som(nin, map_size)
creates a SOM net
28 | with input dimension (i.e. data dimension) nin
and map dimensions
29 | map_size
. Only two-dimensional maps are currently implemented.
30 |
31 | The fields in net
are
32 |
33 | 34 | type = 'som' 35 | nin = number of inputs 36 | map_dim = dimension of map (constrained to be 2) 37 | map_size = grid size: number of nodes in each dimension 38 | num_nodes = number of nodes: the product of values in map_size 39 | map = map_dim+1 dimensional array containing nodes 40 | inode_dist = map of inter-node distances using Manhatten metric 41 |42 | 43 | 44 |
The map contains the node vectors arranged column-wise in the first 45 | dimension of the array. 46 | 47 |
kmeans
, somfwd
, somtrain
Copyright (c) Ian T Nabney (1996-9) 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /help/somfwd.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | 20 | d2 = somfwd(net, x) 21 |22 | 23 | 24 |
d2 = somfwd(net, x)
propagates the data matrix x
through
28 | a SOM net
, returning the squared distance matrix d2
with
29 | dimension nin
by num_nodes
. The $i$th row represents the
30 | squared Euclidean distance to each of the nodes of the SOM.
31 |
32 | [d2, win_nodes] = somfwd(net, x)
also returns the indices of the
33 | winning nodes for each pattern.
34 |
35 |
The following code fragment creates a SOM with a $5times 5$ map for an 40 | 8-dimensional data space. It then applies the test data to the map. 41 |
42 | 43 | net = som(8, [5, 5]); 44 | [d2, wn] = somfwd(net, test_data); 45 |46 | 47 | 48 |
som
, somtrain
Copyright (c) Ian T Nabney (1996-9) 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /help/sompak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | c = sompak(net) 20 |21 | 22 | 23 |
c = sompak(net)
takes a SOM data structure net
and
27 | combines the node weights into a matrix of centres
28 | c
where each row represents the node vector.
29 |
30 | The ordering of the parameters in w
is defined by the indexing of the
31 | multi-dimensional array net.map
.
32 |
33 |
som
, somunpak
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /help/somunpak.htm: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | net = somunpak(net, w) 20 |21 | 22 | 23 |
net = somunpak(net, w)
takes a SOM data structure net
and
27 | weight matrix w
(each node represented by a row) and
28 | puts the nodes back into the multi-dimensional array net.map
.
29 |
30 | The ordering of the parameters in w
is defined by the indexing of the
31 | multi-dimensional array net.map
.
32 |
33 |
som
, sompak
Copyright (c) Ian T Nabney (1996-9) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /hintmat.m: -------------------------------------------------------------------------------- 1 | function [xvals, yvals, color] = hintmat(w); 2 | %HINTMAT Evaluates the coordinates of the patches for a Hinton diagram. 3 | % 4 | % Description 5 | % [xvals, yvals, color] = hintmat(w) 6 | % takes a matrix W and returns coordinates XVALS, YVALS for the 7 | % patches comrising the Hinton diagram, together with a vector COLOR 8 | % labelling the color (black or white) of the corresponding elements 9 | % according to their sign. 10 | % 11 | % See also 12 | % HINTON 13 | % 14 | 15 | % Copyright (c) Ian T Nabney (1996-2001) 16 | 17 | % Set scale to be up to 0.9 of maximum absolute weight value, where scale 18 | % defined so that area of box proportional to weight value. 19 | 20 | w = flipud(w); 21 | [nrows, ncols] = size(w); 22 | 23 | scale = 0.45*sqrt(abs(w)/max(max(abs(w)))); 24 | scale = scale(:); 25 | color = 0.5*(sign(w(:)) + 3); 26 | 27 | delx = 1; 28 | dely = 1; 29 | [X, Y] = meshgrid(0.5*delx:delx:(ncols-0.5*delx), 0.5*dely:dely:(nrows-0.5*dely)); 30 | 31 | % Now convert from matrix format to column vector format, and then duplicate 32 | % columns with appropriate offsets determined by normalized weight magnitudes. 33 | 34 | xtemp = X(:); 35 | ytemp = Y(:); 36 | 37 | xvals = [xtemp-delx*scale, xtemp+delx*scale, ... 38 | xtemp+delx*scale, xtemp-delx*scale]; 39 | yvals = [ytemp-dely*scale, ytemp-dely*scale, ... 40 | ytemp+dely*scale, ytemp+dely*scale]; 41 | 42 | -------------------------------------------------------------------------------- /histp.m: -------------------------------------------------------------------------------- 1 | function h = histp(x, xmin, xmax, nbins) 2 | %HISTP Histogram estimate of 1-dimensional probability distribution. 3 | % 4 | % Description 5 | % 6 | % HISTP(X, XMIN, XMAX, NBINS) takes a column vector X of data values 7 | % and generates a normalized histogram plot of the distribution. The 8 | % histogram has NBINS bins lying in the range XMIN to XMAX. 9 | % 10 | % H = HISTP(...) returns a vector of patch handles. 11 | % 12 | % See also 13 | % DEMGAUSS 14 | % 15 | 16 | % Copyright (c) Ian T Nabney (1996-2001) 17 | 18 | ndata = length(x); 19 | 20 | bins = linspace(xmin, xmax, nbins); 21 | 22 | binwidth = (xmax - xmin)/nbins; 23 | 24 | num = hist(x, bins); 25 | 26 | num = num/(ndata*binwidth); 27 | 28 | h = bar(bins, num, 0.6); 29 | 30 | -------------------------------------------------------------------------------- /knn.m: -------------------------------------------------------------------------------- 1 | function net = knn(nin, nout, k, tr_in, tr_targets) 2 | %KNN Creates a K-nearest-neighbour classifier. 3 | % 4 | % Description 5 | % NET = KNN(NIN, NOUT, K, TR_IN, TR_TARGETS) creates a KNN model NET 6 | % with input dimension NIN, output dimension NOUT and K neighbours. 7 | % The training data is also stored in the data structure and the 8 | % targets are assumed to be using a 1-of-N coding. 9 | % 10 | % The fields in NET are 11 | % type = 'knn' 12 | % nin = number of inputs 13 | % nout = number of outputs 14 | % tr_in = training input data 15 | % tr_targets = training target data 16 | % 17 | % See also 18 | % KMEANS, KNNFWD 19 | % 20 | 21 | % Copyright (c) Ian T Nabney (1996-2001) 22 | 23 | 24 | net.type = 'knn'; 25 | net.nin = nin; 26 | net.nout = nout; 27 | net.k = k; 28 | errstring = consist(net, 'knn', tr_in, tr_targets); 29 | if ~isempty(errstring) 30 | error(errstring); 31 | end 32 | net.tr_in = tr_in; 33 | net.tr_targets = tr_targets; 34 | 35 | -------------------------------------------------------------------------------- /linef.m: -------------------------------------------------------------------------------- 1 | function y = linef(lambda, fn, x, d, varargin) 2 | %LINEF Calculate function value along a line. 3 | % 4 | % Description 5 | % LINEF(LAMBDA, FN, X, D) calculates the value of the function FN at 6 | % the point X+LAMBDA*D. Here X is a row vector and LAMBDA is a scalar. 7 | % 8 | % LINEF(LAMBDA, FN, X, D, P1, P2, ...) allows additional arguments to 9 | % be passed to FN(). This function is used for convenience in some of 10 | % the optimisation routines. 11 | % 12 | % See also 13 | % GRADCHEK, LINEMIN 14 | % 15 | 16 | % Copyright (c) Ian T Nabney (1996-2001) 17 | 18 | % Check function string 19 | fn = fcnchk(fn, length(varargin)); 20 | 21 | y = feval(fn, x+lambda.*d, varargin{:}); 22 | -------------------------------------------------------------------------------- /maxitmess.m: -------------------------------------------------------------------------------- 1 | function s = maxitmess() 2 | %MAXITMESS Create a standard error message when training reaches max. iterations. 3 | % 4 | % Description 5 | % S = MAXITMESS returns a standard string that it used by training 6 | % algorithms when the maximum number of iterations (as specified in 7 | % OPTIONS(14) is reached. 8 | % 9 | % See also 10 | % CONJGRAD, GLMTRAIN, GMMEM, GRADDESC, GTMEM, KMEANS, OLGD, QUASINEW, SCG 11 | % 12 | 13 | % Copyright (c) Ian T Nabney (1996-2001) 14 | 15 | s = 'Maximum number of iterations has been exceeded'; 16 | -------------------------------------------------------------------------------- /mdn2gmm.m: -------------------------------------------------------------------------------- 1 | function gmmmixes = mdn2gmm(mdnmixes) 2 | %MDN2GMM Converts an MDN mixture data structure to array of GMMs. 3 | % 4 | % Description 5 | % GMMMIXES = MDN2GMM(MDNMIXES) takes an MDN mixture data structure 6 | % MDNMIXES containing three matrices (for priors, centres and 7 | % variances) where each row represents the corresponding parameter 8 | % values for a different mixture model and creates an array of GMMs. 9 | % These can then be used with the standard Netlab Gaussian mixture 10 | % model functions. 11 | % 12 | % See also 13 | % GMM, MDN, MDNFWD 14 | % 15 | 16 | % Copyright (c) Ian T Nabney (1996-2001) 17 | % David J Evans (1998) 18 | 19 | % Check argument for consistency 20 | errstring = consist(mdnmixes, 'mdnmixes'); 21 | if ~isempty(errstring) 22 | error(errstring); 23 | end 24 | 25 | nmixes = size(mdnmixes.centres, 1); 26 | % Construct ndata structures containing the mixture model information. 27 | % First allocate the memory. 28 | tempmix = gmm(mdnmixes.dim_target, mdnmixes.ncentres, 'spherical'); 29 | f = fieldnames(tempmix); 30 | gmmmixes = cell(size(f, 1), 1, nmixes); 31 | gmmmixes = cell2struct(gmmmixes, f,1); 32 | 33 | % Then fill each structure in turn using gmmunpak. Assume that spherical 34 | % covariance structure is used. 35 | for i = 1:nmixes 36 | centres = reshape(mdnmixes.centres(i, :), mdnmixes.dim_target, ... 37 | mdnmixes.ncentres)'; 38 | gmmmixes(i) = gmmunpak(tempmix, [mdnmixes.mixcoeffs(i,:), ... 39 | centres(:)', mdnmixes.covars(i,:)]); 40 | end 41 | 42 | -------------------------------------------------------------------------------- /mdnerr.m: -------------------------------------------------------------------------------- 1 | function e = mdnerr(net, x, t) 2 | %MDNERR Evaluate error function for Mixture Density Network. 3 | % 4 | % Description 5 | % E = MDNERR(NET, X, T) takes a mixture density network data structure 6 | % NET, a matrix X of input vectors and a matrix T of target vectors, 7 | % and evaluates the error function E. The error function is the 8 | % negative log likelihood of the target data under the conditional 9 | % density given by the mixture model parameterised by the MLP. Each 10 | % row of X corresponds to one input vector and each row of T 11 | % corresponds to one target vector. 12 | % 13 | % See also 14 | % MDN, MDNFWD, MDNGRAD 15 | % 16 | 17 | % Copyright (c) Ian T Nabney (1996-2001) 18 | % David J Evans (1998) 19 | 20 | % Check arguments for consistency 21 | errstring = consist(net, 'mdn', x, t); 22 | if ~isempty(errstring) 23 | error(errstring); 24 | end 25 | 26 | % Get the output mixture models 27 | mixparams = mdnfwd(net, x); 28 | 29 | % Compute the probabilities of mixtures 30 | probs = mdnprob(mixparams, t); 31 | % Compute the error 32 | e = sum( -log(max(eps, sum(probs, 2)))); 33 | 34 | -------------------------------------------------------------------------------- /mdnnet.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sods/netlab/336524fcd50746519f675c29f6bf181ca6ec8c12/mdnnet.mat -------------------------------------------------------------------------------- /mdnpak.m: -------------------------------------------------------------------------------- 1 | function w = mdnpak(net) 2 | %MDNPAK Combines weights and biases into one weights vector. 3 | % 4 | % Description 5 | % W = MDNPAK(NET) takes a mixture density network data structure NET 6 | % and combines the network weights into a single row vector W. 7 | % 8 | % See also 9 | % MDN, MDNUNPAK, MDNFWD, MDNERR, MDNGRAD 10 | % 11 | 12 | % Copyright (c) Ian T Nabney (1996-2001) 13 | % David J Evans (1998) 14 | 15 | errstring = consist(net, 'mdn'); 16 | if ~errstring 17 | error(errstring); 18 | end 19 | w = mlppak(net.mlp); 20 | -------------------------------------------------------------------------------- /mdnpost.m: -------------------------------------------------------------------------------- 1 | function [post, a] = mdnpost(mixparams, t) 2 | %MDNPOST Computes the posterior probability for each MDN mixture component. 3 | % 4 | % Description 5 | % POST = MDNPOST(MIXPARAMS, T) computes the posterior probability 6 | % P(J|T) of each data vector in T under the Gaussian mixture model 7 | % represented by the corresponding entries in MIXPARAMS. Each row of T 8 | % represents a single vector. 9 | % 10 | % [POST, A] = MDNPOST(MIXPARAMS, T) also computes the activations A 11 | % (i.e. the probability P(T|J) of the data conditioned on each 12 | % component density) for a Gaussian mixture model. 13 | % 14 | % See also 15 | % MDNGRAD, MDNPROB 16 | % 17 | 18 | % Copyright (c) Ian T Nabney (1996-2001) 19 | % David J Evans (1998) 20 | 21 | [prob a] = mdnprob(mixparams, t); 22 | 23 | s = sum(prob, 2); 24 | % Set any zeros to one before dividing 25 | s = s + (s==0); 26 | post = prob./(s*ones(1, mixparams.ncentres)); 27 | -------------------------------------------------------------------------------- /mdnunpak.m: -------------------------------------------------------------------------------- 1 | function net = mdnunpak(net, w) 2 | %MDNUNPAK Separates weights vector into weight and bias matrices. 3 | % 4 | % Description 5 | % NET = MDNUNPAK(NET, W) takes an mdn network data structure NET and a 6 | % weight vector W, and returns a network data structure identical to 7 | % the input network, except that the weights in the MLP sub-structure 8 | % are set to the corresponding elements of W. 9 | % 10 | % See also 11 | % MDN, MDNPAK, MDNFWD, MDNERR, MDNGRAD 12 | % 13 | 14 | % Copyright (c) Ian T Nabney (1996-2001) 15 | % David J Evans (1998) 16 | 17 | errstring = consist(net, 'mdn'); 18 | if ~errstring 19 | error(errstring); 20 | end 21 | if net.nwts ~= length(w) 22 | error('Invalid weight vector length') 23 | end 24 | 25 | net.mlp = mlpunpak(net.mlp, w); 26 | -------------------------------------------------------------------------------- /mlpbkp.m: -------------------------------------------------------------------------------- 1 | function g = mlpbkp(net, x, z, deltas) 2 | %MLPBKP Backpropagate gradient of error function for 2-layer network. 3 | % 4 | % Description 5 | % G = MLPBKP(NET, X, Z, DELTAS) takes a network data structure NET 6 | % together with a matrix X of input vectors, a matrix Z of hidden unit 7 | % activations, and a matrix DELTAS of the gradient of the error 8 | % function with respect to the values of the output units (i.e. the 9 | % summed inputs to the output units, before the activation function is 10 | % applied). The return value is the gradient G of the error function 11 | % with respect to the network weights. Each row of X corresponds to one 12 | % input vector. 13 | % 14 | % This function is provided so that the common backpropagation 15 | % algorithm can be used by multi-layer perceptron network models to 16 | % compute gradients for mixture density networks as well as standard 17 | % error functions. 18 | % 19 | % See also 20 | % MLP, MLPGRAD, MLPDERIV, MDNGRAD 21 | % 22 | 23 | % Copyright (c) Ian T Nabney (1996-2001) 24 | 25 | % Evaluate second-layer gradients. 26 | gw2 = z'*deltas; 27 | gb2 = sum(deltas, 1); 28 | 29 | % Now do the backpropagation. 30 | delhid = deltas*net.w2'; 31 | delhid = delhid.*(1.0 - z.*z); 32 | 33 | % Finally, evaluate the first-layer gradients. 34 | gw1 = x'*delhid; 35 | gb1 = sum(delhid, 1); 36 | 37 | g = [gw1(:)', gb1, gw2(:)', gb2]; 38 | -------------------------------------------------------------------------------- /mlpderiv.m: -------------------------------------------------------------------------------- 1 | function g = mlpderiv(net, x) 2 | %MLPDERIV Evaluate derivatives of network outputs with respect to weights. 3 | % 4 | % Description 5 | % G = MLPDERIV(NET, X) takes a network data structure NET and a matrix 6 | % of input vectors X and returns a three-index matrix G whose I, J, K 7 | % element contains the derivative of network output K with respect to 8 | % weight or bias parameter J for input pattern I. The ordering of the 9 | % weight and bias parameters is defined by MLPUNPAK. 10 | % 11 | % See also 12 | % MLP, MLPPAK, MLPGRAD, MLPBKP 13 | % 14 | 15 | % Copyright (c) Ian T Nabney (1996-2001) 16 | 17 | % Check arguments for consistency 18 | errstring = consist(net, 'mlp', x); 19 | if ~isempty(errstring); 20 | error(errstring); 21 | end 22 | 23 | [y, z] = mlpfwd(net, x); 24 | 25 | ndata = size(x, 1); 26 | 27 | if isfield(net, 'mask') 28 | nwts = size(find(net.mask), 1); 29 | temp = zeros(1, net.nwts); 30 | else 31 | nwts = net.nwts; 32 | end 33 | 34 | g = zeros(ndata, nwts, net.nout); 35 | for k = 1 : net.nout 36 | delta = zeros(1, net.nout); 37 | delta(1, k) = 1; 38 | for n = 1 : ndata 39 | if isfield(net, 'mask') 40 | temp = mlpbkp(net, x(n, :), z(n, :), delta); 41 | g(n, :, k) = temp(logical(net.mask)); 42 | else 43 | g(n, :, k) = mlpbkp(net, x(n, :), z(n, :),... 44 | delta); 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /mlpevfwd.m: -------------------------------------------------------------------------------- 1 | function [y, extra, invhess] = mlpevfwd(net, x, t, x_test, invhess) 2 | %MLPEVFWD Forward propagation with evidence for MLP 3 | % 4 | % Description 5 | % Y = MLPEVFWD(NET, X, T, X_TEST) takes a network data structure NET 6 | % together with the input X and target T training data and input test 7 | % data X_TEST. It returns the normal forward propagation through the 8 | % network Y together with a matrix EXTRA which consists of error bars 9 | % (variance) for a regression problem or moderated outputs for a 10 | % classification problem. The optional argument (and return value) 11 | % INVHESS is the inverse of the network Hessian computed on the 12 | % training data inputs and targets. Passing it in avoids recomputing 13 | % it, which can be a significant saving for large training sets. 14 | % 15 | % See also 16 | % FEVBAYES 17 | % 18 | 19 | % Copyright (c) Ian T Nabney (1996-2001) 20 | 21 | [y, z, a] = mlpfwd(net, x_test); 22 | if nargin == 4 23 | [extra, invhess] = fevbayes(net, y, a, x, t, x_test); 24 | else 25 | [extra, invhess] = fevbayes(net, y, a, x, t, x_test, invhess); 26 | end -------------------------------------------------------------------------------- /mlpgrad.m: -------------------------------------------------------------------------------- 1 | function [g, gdata, gprior] = mlpgrad(net, x, t) 2 | %MLPGRAD Evaluate gradient of error function for 2-layer network. 3 | % 4 | % Description 5 | % G = MLPGRAD(NET, X, T) takes a network data structure NET together 6 | % with a matrix X of input vectors and a matrix T of target vectors, 7 | % and evaluates the gradient G of the error function with respect to 8 | % the network weights. The error funcion corresponds to the choice of 9 | % output unit activation function. Each row of X corresponds to one 10 | % input vector and each row of T corresponds to one target vector. 11 | % 12 | % [G, GDATA, GPRIOR] = MLPGRAD(NET, X, T) also returns separately the 13 | % data and prior contributions to the gradient. In the case of multiple 14 | % groups in the prior, GPRIOR is a matrix with a row for each group and 15 | % a column for each weight parameter. 16 | % 17 | % See also 18 | % MLP, MLPPAK, MLPUNPAK, MLPFWD, MLPERR, MLPBKP 19 | % 20 | 21 | % Copyright (c) Ian T Nabney (1996-2001) 22 | 23 | % Check arguments for consistency 24 | errstring = consist(net, 'mlp', x, t); 25 | if ~isempty(errstring); 26 | error(errstring); 27 | end 28 | [y, z] = mlpfwd(net, x); 29 | delout = y - t; 30 | 31 | gdata = mlpbkp(net, x, z, delout); 32 | 33 | [g, gdata, gprior] = gbayes(net, gdata); 34 | -------------------------------------------------------------------------------- /mlpinit.m: -------------------------------------------------------------------------------- 1 | function net = mlpinit(net, prior) 2 | %MLPINIT Initialise the weights in a 2-layer feedforward network. 3 | % 4 | % Description 5 | % 6 | % NET = MLPINIT(NET, PRIOR) takes a 2-layer feedforward network NET and 7 | % sets the weights and biases by sampling from a Gaussian distribution. 8 | % If PRIOR is a scalar, then all of the parameters (weights and biases) 9 | % are sampled from a single isotropic Gaussian with inverse variance 10 | % equal to PRIOR. If PRIOR is a data structure of the kind generated by 11 | % MLPPRIOR, then the parameters are sampled from multiple Gaussians 12 | % according to their groupings (defined by the INDEX field) with 13 | % corresponding variances (defined by the ALPHA field). 14 | % 15 | % See also 16 | % MLP, MLPPRIOR, MLPPAK, MLPUNPAK 17 | % 18 | 19 | % Copyright (c) Ian T Nabney (1996-2001) 20 | 21 | if isstruct(prior) 22 | sig = 1./sqrt(prior.index*prior.alpha); 23 | w = sig'.*randn(1, net.nwts); 24 | elseif size(prior) == [1 1] 25 | w = randn(1, net.nwts).*sqrt(1/prior); 26 | else 27 | error('prior must be a scalar or a structure'); 28 | end 29 | 30 | net = mlpunpak(net, w); 31 | 32 | -------------------------------------------------------------------------------- /mlppak.m: -------------------------------------------------------------------------------- 1 | function w = mlppak(net) 2 | %MLPPAK Combines weights and biases into one weights vector. 3 | % 4 | % Description 5 | % W = MLPPAK(NET) takes a network data structure NET and combines the 6 | % component weight matrices bias vectors into a single row vector W. 7 | % The facility to switch between these two representations for the 8 | % network parameters is useful, for example, in training a network by 9 | % error function minimization, since a single vector of parameters can 10 | % be handled by general-purpose optimization routines. 11 | % 12 | % The ordering of the paramters in W is defined by 13 | % w = [net.w1(:)', net.b1, net.w2(:)', net.b2]; 14 | % where W1 is the first-layer weight matrix, B1 is the first-layer 15 | % bias vector, W2 is the second-layer weight matrix, and B2 is the 16 | % second-layer bias vector. 17 | % 18 | % See also 19 | % MLP, MLPUNPAK, MLPFWD, MLPERR, MLPBKP, MLPGRAD 20 | % 21 | 22 | % Copyright (c) Ian T Nabney (1996-2001) 23 | 24 | % Check arguments for consistency 25 | errstring = consist(net, 'mlp'); 26 | if ~isempty(errstring); 27 | error(errstring); 28 | end 29 | 30 | w = [net.w1(:)', net.b1, net.w2(:)', net.b2]; 31 | 32 | -------------------------------------------------------------------------------- /mlptrain.m: -------------------------------------------------------------------------------- 1 | function [net, error] = mlptrain(net, x, t, its); 2 | %MLPTRAIN Utility to train an MLP network for demtrain 3 | % 4 | % Description 5 | % 6 | % [NET, ERROR] = MLPTRAIN(NET, X, T, ITS) trains a network data 7 | % structure NET using the scaled conjugate gradient algorithm for ITS 8 | % cycles with input data X, target data T. 9 | % 10 | % See also 11 | % DEMTRAIN, SCG, NETOPT 12 | % 13 | 14 | % Copyright (c) Ian T Nabney (1996-2001) 15 | 16 | options = zeros(1,18); 17 | options(1) = -1; % To prevent any messages at all 18 | options(9) = 0; 19 | options(14) = its; 20 | 21 | [net, options] = netopt(net, options, x, t, 'scg'); 22 | 23 | error = options(8); 24 | 25 | -------------------------------------------------------------------------------- /mlpunpak.m: -------------------------------------------------------------------------------- 1 | function net = mlpunpak(net, w) 2 | %MLPUNPAK Separates weights vector into weight and bias matrices. 3 | % 4 | % Description 5 | % NET = MLPUNPAK(NET, W) takes an mlp network data structure NET and a 6 | % weight vector W, and returns a network data structure identical to 7 | % the input network, except that the first-layer weight matrix W1, the 8 | % first-layer bias vector B1, the second-layer weight matrix W2 and the 9 | % second-layer bias vector B2 have all been set to the corresponding 10 | % elements of W. 11 | % 12 | % See also 13 | % MLP, MLPPAK, MLPFWD, MLPERR, MLPBKP, MLPGRAD 14 | % 15 | 16 | % Copyright (c) Ian T Nabney (1996-2001) 17 | 18 | % Check arguments for consistency 19 | errstring = consist(net, 'mlp'); 20 | if ~isempty(errstring); 21 | error(errstring); 22 | end 23 | 24 | if net.nwts ~= length(w) 25 | error('Invalid weight vector length') 26 | end 27 | 28 | nin = net.nin; 29 | nhidden = net.nhidden; 30 | nout = net.nout; 31 | 32 | mark1 = nin*nhidden; 33 | net.w1 = reshape(w(1:mark1), nin, nhidden); 34 | mark2 = mark1 + nhidden; 35 | net.b1 = reshape(w(mark1 + 1: mark2), 1, nhidden); 36 | mark3 = mark2 + nhidden*nout; 37 | net.w2 = reshape(w(mark2 + 1: mark3), nhidden, nout); 38 | mark4 = mark3 + nout; 39 | net.b2 = reshape(w(mark3 + 1: mark4), 1, nout); 40 | -------------------------------------------------------------------------------- /netderiv.m: -------------------------------------------------------------------------------- 1 | function g = netderiv(w, net, x) 2 | %NETDERIV Evaluate derivatives of network outputs by weights generically. 3 | % 4 | % Description 5 | % 6 | % G = NETDERIV(W, NET, X) takes a weight vector W and a network data 7 | % structure NET, together with the matrix X of input vectors, and 8 | % returns the gradient of the outputs with respect to the weights 9 | % evaluated at W. 10 | % 11 | % See also 12 | % NETEVFWD, NETOPT 13 | % 14 | 15 | % Copyright (c) Ian T Nabney (1996-2001) 16 | 17 | fstr = [net.type, 'deriv']; 18 | net = netunpak(net, w); 19 | g = feval(fstr, net, x); 20 | -------------------------------------------------------------------------------- /neterr.m: -------------------------------------------------------------------------------- 1 | function [e, varargout] = neterr(w, net, x, t) 2 | %NETERR Evaluate network error function for generic optimizers 3 | % 4 | % Description 5 | % 6 | % E = NETERR(W, NET, X, T) takes a weight vector W and a network data 7 | % structure NET, together with the matrix X of input vectors and the 8 | % matrix T of target vectors, and returns the value of the error 9 | % function evaluated at W. 10 | % 11 | % [E, VARARGOUT] = NETERR(W, NET, X, T) also returns any additional 12 | % return values from the error function. 13 | % 14 | % See also 15 | % NETGRAD, NETHESS, NETOPT 16 | % 17 | 18 | % Copyright (c) Ian T Nabney (1996-2001) 19 | 20 | errstr = [net.type, 'err']; 21 | net = netunpak(net, w); 22 | 23 | [s{1:nargout}] = feval(errstr, net, x, t); 24 | e = s{1}; 25 | if nargout > 1 26 | for i = 2:nargout 27 | varargout{i-1} = s{i}; 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /netevfwd.m: -------------------------------------------------------------------------------- 1 | function [y, extra, invhess] = netevfwd(w, net, x, t, x_test, invhess) 2 | %NETEVFWD Generic forward propagation with evidence for network 3 | % 4 | % Description 5 | % [Y, EXTRA] = NETEVFWD(W, NET, X, T, X_TEST) takes a network data 6 | % structure NET together with the input X and target T training data 7 | % and input test data X_TEST. It returns the normal forward propagation 8 | % through the network Y together with a matrix EXTRA which consists of 9 | % error bars (variance) for a regression problem or moderated outputs 10 | % for a classification problem. 11 | % 12 | % The optional argument (and return value) INVHESS is the inverse of 13 | % the network Hessian computed on the training data inputs and targets. 14 | % Passing it in avoids recomputing it, which can be a significant 15 | % saving for large training sets. 16 | % 17 | % See also 18 | % MLPEVFWD, RBFEVFWD, GLMEVFWD, FEVBAYES 19 | % 20 | 21 | % Copyright (c) Ian T Nabney (1996-2001) 22 | 23 | func = [net.type, 'evfwd']; 24 | net = netunpak(net, w); 25 | if nargin == 5 26 | [y, extra, invhess] = feval(func, net, x, t, x_test); 27 | else 28 | [y, extra, invhess] = feval(func, net, x, t, x_test, invhess); 29 | end 30 | -------------------------------------------------------------------------------- /netgrad.m: -------------------------------------------------------------------------------- 1 | function g = netgrad(w, net, x, t) 2 | %NETGRAD Evaluate network error gradient for generic optimizers 3 | % 4 | % Description 5 | % 6 | % G = NETGRAD(W, NET, X, T) takes a weight vector W and a network data 7 | % structure NET, together with the matrix X of input vectors and the 8 | % matrix T of target vectors, and returns the gradient of the error 9 | % function evaluated at W. 10 | % 11 | % See also 12 | % MLP, NETERR, NETOPT 13 | % 14 | 15 | % Copyright (c) Ian T Nabney (1996-2001) 16 | 17 | gradstr = [net.type, 'grad']; 18 | 19 | net = netunpak(net, w); 20 | 21 | g = feval(gradstr, net, x, t); 22 | -------------------------------------------------------------------------------- /nethess.m: -------------------------------------------------------------------------------- 1 | function [h, varargout] = nethess(w, net, x, t, varargin) 2 | %NETHESS Evaluate network Hessian 3 | % 4 | % Description 5 | % 6 | % H = NETHESS(W, NET, X, T) takes a weight vector W and a network data 7 | % structure NET, together with the matrix X of input vectors and the 8 | % matrix T of target vectors, and returns the value of the Hessian 9 | % evaluated at W. 10 | % 11 | % [E, VARARGOUT] = NETHESS(W, NET, X, T, VARARGIN) also returns any 12 | % additional return values from the network Hessian function, and 13 | % passes additional arguments to that function. 14 | % 15 | % See also 16 | % NETERR, NETGRAD, NETOPT 17 | % 18 | 19 | % Copyright (c) Ian T Nabney (1996-2001) 20 | 21 | hess_str = [net.type, 'hess']; 22 | 23 | net = netunpak(net, w); 24 | 25 | [s{1:nargout}] = feval(hess_str, net, x, t, varargin{:}); 26 | h = s{1}; 27 | for i = 2:nargout 28 | varargout{i-1} = s{i}; 29 | end 30 | -------------------------------------------------------------------------------- /netinit.m: -------------------------------------------------------------------------------- 1 | function net = netinit(net, prior) 2 | %NETINIT Initialise the weights in a network. 3 | % 4 | % Description 5 | % 6 | % NET = NETINIT(NET, PRIOR) takes a network data structure NET and sets 7 | % the weights and biases by sampling from a Gaussian distribution. If 8 | % PRIOR is a scalar, then all of the parameters (weights and biases) 9 | % are sampled from a single isotropic Gaussian with inverse variance 10 | % equal to PRIOR. If PRIOR is a data structure of the kind generated by 11 | % MLPPRIOR, then the parameters are sampled from multiple Gaussians 12 | % according to their groupings (defined by the INDEX field) with 13 | % corresponding variances (defined by the ALPHA field). 14 | % 15 | % See also 16 | % MLPPRIOR, NETUNPAK, RBFPRIOR 17 | % 18 | 19 | % Copyright (c) Ian T Nabney (1996-2001) 20 | 21 | if isstruct(prior) 22 | if (isfield(net, 'mask')) 23 | if find(sum(prior.index, 2)) ~= find(net.mask) 24 | error('Index does not match mask'); 25 | end 26 | sig = sqrt(prior.index*prior.alpha); 27 | % Weights corresponding to zeros in mask will not be used anyway 28 | % Set their priors to one to avoid division by zero 29 | sig = sig + (sig == 0); 30 | sig = 1./sqrt(sig); 31 | else 32 | sig = 1./sqrt(prior.index*prior.alpha); 33 | end 34 | w = sig'.*randn(1, net.nwts); 35 | elseif size(prior) == [1 1] 36 | w = randn(1, net.nwts).*sqrt(1/prior); 37 | else 38 | error('prior must be a scalar or a structure'); 39 | end 40 | 41 | if (isfield(net, 'mask')) 42 | w = w(logical(net.mask)); 43 | end 44 | net = netunpak(net, w); 45 | 46 | -------------------------------------------------------------------------------- /netlogo.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sods/netlab/336524fcd50746519f675c29f6bf181ca6ec8c12/netlogo.mat -------------------------------------------------------------------------------- /netpak.m: -------------------------------------------------------------------------------- 1 | function w = netpak(net) 2 | %NETPAK Combines weights and biases into one weights vector. 3 | % 4 | % Description 5 | % W = NETPAK(NET) takes a network data structure NET and combines the 6 | % component weight matrices into a single row vector W. The facility 7 | % to switch between these two representations for the network 8 | % parameters is useful, for example, in training a network by error 9 | % function minimization, since a single vector of parameters can be 10 | % handled by general-purpose optimization routines. This function also 11 | % takes into account a MASK defined as a field in NET by removing any 12 | % weights that correspond to entries of 0 in the mask. 13 | % 14 | % See also 15 | % NET, NETUNPAK, NETFWD, NETERR, NETGRAD 16 | % 17 | 18 | % Copyright (c) Ian T Nabney (1996-2001) 19 | 20 | pakstr = [net.type, 'pak']; 21 | w = feval(pakstr, net); 22 | % Return masked subset of weights 23 | if (isfield(net, 'mask')) 24 | w = w(logical(net.mask)); 25 | end -------------------------------------------------------------------------------- /netunpak.m: -------------------------------------------------------------------------------- 1 | function net = netunpak(net, w) 2 | %NETUNPAK Separates weights vector into weight and bias matrices. 3 | % 4 | % Description 5 | % NET = NETUNPAK(NET, W) takes an net network data structure NET and a 6 | % weight vector W, and returns a network data structure identical to 7 | % the input network, except that the componenet weight matrices have 8 | % all been set to the corresponding elements of W. If there is a MASK 9 | % field in the NET data structure, then the weights in W are placed in 10 | % locations corresponding to non-zero entries in the mask (so W should 11 | % have the same length as the number of non-zero entries in the MASK). 12 | % 13 | % See also 14 | % NETPAK, NETFWD, NETERR, NETGRAD 15 | % 16 | 17 | % Copyright (c) Ian T Nabney (1996-2001) 18 | 19 | unpakstr = [net.type, 'unpak']; 20 | 21 | % Check if we are being passed a masked set of weights 22 | if (isfield(net, 'mask')) 23 | if length(w) ~= size(find(net.mask), 1) 24 | error('Weight vector length does not match mask length') 25 | end 26 | % Do a full pack of all current network weights 27 | pakstr = [net.type, 'pak']; 28 | fullw = feval(pakstr, net); 29 | % Replace current weights with new ones 30 | fullw(logical(net.mask)) = w; 31 | w = fullw; 32 | end 33 | 34 | net = feval(unpakstr, net, w); -------------------------------------------------------------------------------- /pca.m: -------------------------------------------------------------------------------- 1 | function [PCcoeff, PCvec] = pca(data, N) 2 | %PCA Principal Components Analysis 3 | % 4 | % Description 5 | % PCCOEFF = PCA(DATA) computes the eigenvalues of the covariance 6 | % matrix of the dataset DATA and returns them as PCCOEFF. These 7 | % coefficients give the variance of DATA along the corresponding 8 | % principal components. 9 | % 10 | % PCCOEFF = PCA(DATA, N) returns the largest N eigenvalues. 11 | % 12 | % [PCCOEFF, PCVEC] = PCA(DATA) returns the principal components as well 13 | % as the coefficients. This is considerably more computationally 14 | % demanding than just computing the eigenvalues. 15 | % 16 | % See also 17 | % EIGDEC, GTMINIT, PPCA 18 | % 19 | 20 | % Copyright (c) Ian T Nabney (1996-2001) 21 | 22 | if nargin == 1 23 | N = size(data, 2); 24 | end 25 | 26 | if nargout == 1 27 | evals_only = logical(1); 28 | else 29 | evals_only = logical(0); 30 | end 31 | 32 | if N ~= round(N) | N < 1 | N > size(data, 2) 33 | error('Number of PCs must be integer, >0, < dim'); 34 | end 35 | 36 | % Find the sorted eigenvalues of the data covariance matrix 37 | if evals_only 38 | PCcoeff = eigdec(cov(data), N); 39 | else 40 | [PCcoeff, PCvec] = eigdec(cov(data), N); 41 | end 42 | 43 | -------------------------------------------------------------------------------- /plotmat.m: -------------------------------------------------------------------------------- 1 | function plotmat(matrix, textcolour, gridcolour, fontsize) 2 | %PLOTMAT Display a matrix. 3 | % 4 | % Description 5 | % PLOTMAT(MATRIX, TEXTCOLOUR, GRIDCOLOUR, FONTSIZE) displays the matrix 6 | % MATRIX on the current figure. The TEXTCOLOUR and GRIDCOLOUR 7 | % arguments control the colours of the numbers and grid labels 8 | % respectively and should follow the usual Matlab specification. The 9 | % parameter FONTSIZE should be an integer. 10 | % 11 | % See also 12 | % CONFFIG, DEMMLP2 13 | % 14 | 15 | % Copyright (c) Ian T Nabney (1996-2001) 16 | 17 | [m,n]=size(matrix); 18 | for rowCnt=1:m, 19 | for colCnt=1:n, 20 | numberString=num2str(matrix(rowCnt,colCnt)); 21 | text(colCnt-.5,m-rowCnt+.5,numberString, ... 22 | 'HorizontalAlignment','center', ... 23 | 'Color', textcolour, ... 24 | 'FontWeight','bold', ... 25 | 'FontSize', fontsize); 26 | end; 27 | end; 28 | 29 | set(gca,'Box','on', ... 30 | 'Visible','on', ... 31 | 'xLim',[0 n], ... 32 | 'xGrid','on', ... 33 | 'xTickLabel',[], ... 34 | 'xTick',0:n, ... 35 | 'yGrid','on', ... 36 | 'yLim',[0 m], ... 37 | 'yTickLabel',[], ... 38 | 'yTick',0:m, ... 39 | 'DataAspectRatio',[1, 1, 1], ... 40 | 'GridLineStyle',':', ... 41 | 'LineWidth',3, ... 42 | 'XColor',gridcolour, ... 43 | 'YColor',gridcolour); 44 | 45 | -------------------------------------------------------------------------------- /rbfderiv.m: -------------------------------------------------------------------------------- 1 | function g = rbfderiv(net, x) 2 | %RBFDERIV Evaluate derivatives of RBF network outputs with respect to weights. 3 | % 4 | % Description 5 | % G = RBFDERIV(NET, X) takes a network data structure NET and a matrix 6 | % of input vectors X and returns a three-index matrix G whose I, J, K 7 | % element contains the derivative of network output K with respect to 8 | % weight or bias parameter J for input pattern I. The ordering of the 9 | % weight and bias parameters is defined by RBFUNPAK. This function 10 | % also takes into account any mask in the network data structure. 11 | % 12 | % See also 13 | % RBF, RBFPAK, RBFGRAD, RBFBKP 14 | % 15 | 16 | % Copyright (c) Ian T Nabney (1996-2001) 17 | 18 | % Check arguments for consistency 19 | errstring = consist(net, 'rbf', x); 20 | if ~isempty(errstring); 21 | error(errstring); 22 | end 23 | 24 | if ~strcmp(net.outfn, 'linear') 25 | error('Function only implemented for linear outputs') 26 | end 27 | 28 | [y, z, n2] = rbffwd(net, x); 29 | ndata = size(x, 1); 30 | 31 | if isfield(net, 'mask') 32 | nwts = size(find(net.mask), 1); 33 | temp = zeros(1, net.nwts); 34 | else 35 | nwts = net.nwts; 36 | end 37 | 38 | g = zeros(ndata, nwts, net.nout); 39 | for k = 1 : net.nout 40 | delta = zeros(1, net.nout); 41 | delta(1, k) = 1; 42 | for n = 1 : ndata 43 | if isfield(net, 'mask') 44 | temp = rbfbkp(net, x(n, :), z(n, :), n2(n, :), delta); 45 | g(n, :, k) = temp(logical(net.mask)); 46 | else 47 | g(n, :, k) = rbfbkp(net, x(n, :), z(n, :), n2(n, :),... 48 | delta); 49 | end 50 | end 51 | end 52 | 53 | -------------------------------------------------------------------------------- /rbfevfwd.m: -------------------------------------------------------------------------------- 1 | function [y, extra, invhess] = rbfevfwd(net, x, t, x_test, invhess) 2 | %RBFEVFWD Forward propagation with evidence for RBF 3 | % 4 | % Description 5 | % Y = RBFEVFWD(NET, X, T, X_TEST) takes a network data structure NET 6 | % together with the input X and target T training data and input test 7 | % data X_TEST. It returns the normal forward propagation through the 8 | % network Y together with a matrix EXTRA which consists of error bars 9 | % (variance) for a regression problem or moderated outputs for a 10 | % classification problem. 11 | % 12 | % The optional argument (and return value) INVHESS is the inverse of 13 | % the network Hessian computed on the training data inputs and targets. 14 | % Passing it in avoids recomputing it, which can be a significant 15 | % saving for large training sets. 16 | % 17 | % See also 18 | % FEVBAYES 19 | % 20 | 21 | % Copyright (c) Ian T Nabney (1996-2001) 22 | 23 | y = rbffwd(net, x_test); 24 | % RBF outputs must be linear, so just pass them twice (second copy is 25 | % not used 26 | if nargin == 4 27 | [extra, invhess] = fevbayes(net, y, y, x, t, x_test); 28 | else 29 | [extra, invhess] = fevbayes(net, y, y, x, t, x_test, invhess); 30 | end -------------------------------------------------------------------------------- /rbfjacob.m: -------------------------------------------------------------------------------- 1 | function jac = rbfjacob(net, x) 2 | %RBFJACOB Evaluate derivatives of RBF network outputs with respect to inputs. 3 | % 4 | % Description 5 | % G = RBFJACOB(NET, X) takes a network data structure NET and a matrix 6 | % of input vectors X and returns a three-index matrix G whose I, J, K 7 | % element contains the derivative of network output K with respect to 8 | % input parameter J for input pattern I. 9 | % 10 | % See also 11 | % RBF, RBFGRAD, RBFBKP 12 | % 13 | 14 | % Copyright (c) Ian T Nabney (1996-2001) 15 | 16 | % Check arguments for consistency 17 | errstring = consist(net, 'rbf', x); 18 | if ~isempty(errstring); 19 | error(errstring); 20 | end 21 | 22 | if ~strcmp(net.outfn, 'linear') 23 | error('Function only implemented for linear outputs') 24 | end 25 | 26 | [y, z, n2] = rbffwd(net, x); 27 | 28 | ndata = size(x, 1); 29 | jac = zeros(ndata, net.nin, net.nout); 30 | Psi = zeros(net.nin, net.nhidden); 31 | % Calculate derivative of activations wrt n2 32 | switch net.actfn 33 | case 'gaussian' 34 | dz = -z./(ones(ndata, 1)*net.wi); 35 | case 'tps' 36 | dz = 2*(1 + log(n2+(n2==0))); 37 | case 'r4logr' 38 | dz = 2*(n2.*(1+2.*log(n2+(n2==0)))); 39 | otherwise 40 | error(['Unknown activation function ', net.actfn]); 41 | end 42 | 43 | % Ignore biases as they cannot affect Jacobian 44 | for n = 1:ndata 45 | Psi = (ones(net.nin, 1)*dz(n, :)).* ... 46 | (x(n, :)'*ones(1, net.nhidden) - net.c'); 47 | % Now compute the Jacobian 48 | jac(n, :, :) = Psi * net.w2; 49 | end -------------------------------------------------------------------------------- /rbfpak.m: -------------------------------------------------------------------------------- 1 | function w = rbfpak(net) 2 | %RBFPAK Combines all the parameters in an RBF network into one weights vector. 3 | % 4 | % Description 5 | % W = RBFPAK(NET) takes a network data structure NET and combines the 6 | % component parameter matrices into a single row vector W. 7 | % 8 | % See also 9 | % RBFUNPAK, RBF 10 | % 11 | 12 | % Copyright (c) Ian T Nabney (1996-2001) 13 | 14 | errstring = consist(net, 'rbf'); 15 | if ~errstring 16 | error(errstring); 17 | end 18 | 19 | w = [net.c(:)', net.wi, net.w2(:)', net.b2]; 20 | -------------------------------------------------------------------------------- /rbfsetbf.m: -------------------------------------------------------------------------------- 1 | function net = rbfsetbf(net, options, x) 2 | %RBFSETBF Set basis functions of RBF from data. 3 | % 4 | % Description 5 | % NET = RBFSETBF(NET, OPTIONS, X) sets the basis functions of the RBF 6 | % network NET so that they model the unconditional density of the 7 | % dataset X. This is done by training a GMM with spherical covariances 8 | % using GMMEM. The OPTIONS vector is passed to GMMEM. The widths of 9 | % the functions are set by a call to RBFSETFW. 10 | % 11 | % See also 12 | % RBFTRAIN, RBFSETFW, GMMEM 13 | % 14 | 15 | % Copyright (c) Ian T Nabney (1996-2001) 16 | 17 | errstring = consist(net, 'rbf', x); 18 | if ~isempty(errstring) 19 | error(errstring); 20 | end 21 | 22 | % Create a spherical Gaussian mixture model 23 | mix = gmm(net.nin, net.nhidden, 'spherical'); 24 | 25 | % Initialise the parameters from the input data 26 | % Just use a small number of k means iterations 27 | kmoptions = zeros(1, 18); 28 | kmoptions(1) = -1; % Turn off warnings 29 | kmoptions(14) = 5; % Just 5 iterations to get centres roughly right 30 | mix = gmminit(mix, x, kmoptions); 31 | 32 | % Train mixture model using EM algorithm 33 | [mix, options] = gmmem(mix, x, options); 34 | 35 | % Now set the centres of the RBF from the centres of the mixture model 36 | net.c = mix.centres; 37 | 38 | % options(7) gives scale of function widths 39 | net = rbfsetfw(net, options(7)); -------------------------------------------------------------------------------- /rbfsetfw.m: -------------------------------------------------------------------------------- 1 | function net = rbfsetfw(net, scale) 2 | %RBFSETFW Set basis function widths of RBF. 3 | % 4 | % Description 5 | % NET = RBFSETFW(NET, SCALE) sets the widths of the basis functions of 6 | % the RBF network NET. If Gaussian basis functions are used, then the 7 | % variances are set to the largest squared distance between centres if 8 | % SCALE is non-positive and SCALE times the mean distance of each 9 | % centre to its nearest neighbour if SCALE is positive. Non-Gaussian 10 | % basis functions do not have a width. 11 | % 12 | % See also 13 | % RBFTRAIN, RBFSETBF, GMMEM 14 | % 15 | 16 | % Copyright (c) Ian T Nabney (1996-2001) 17 | 18 | % Set the variances to be the largest squared distance between centres 19 | if strcmp(net.actfn, 'gaussian') 20 | cdist = dist2(net.c, net.c); 21 | if scale > 0.0 22 | % Set variance of basis to be scale times average 23 | % distance to nearest neighbour 24 | cdist = cdist + realmax*eye(net.nhidden); 25 | widths = scale*mean(min(cdist)); 26 | else 27 | widths = max(max(cdist)); 28 | end 29 | net.wi = widths * ones(size(net.wi)); 30 | end 31 | -------------------------------------------------------------------------------- /rbfunpak.m: -------------------------------------------------------------------------------- 1 | function net = rbfunpak(net, w) 2 | %RBFUNPAK Separates a vector of RBF weights into its components. 3 | % 4 | % Description 5 | % NET = RBFUNPAK(NET, W) takes an RBF network data structure NET and a 6 | % weight vector W, and returns a network data structure identical to 7 | % the input network, except that the centres C, the widths WI, the 8 | % second-layer weight matrix W2 and the second-layer bias vector B2 9 | % have all been set to the corresponding elements of W. 10 | % 11 | % See also 12 | % RBFPAK, RBF 13 | % 14 | 15 | % Copyright (c) Ian T Nabney (1996-2001) 16 | 17 | % Check arguments for consistency 18 | errstring = consist(net, 'rbf'); 19 | if ~errstring 20 | error(errstring); 21 | end 22 | 23 | if net.nwts ~= length(w) 24 | error('Invalid length of weight vector') 25 | end 26 | 27 | nin = net.nin; 28 | nhidden = net.nhidden; 29 | nout = net.nout; 30 | 31 | mark1 = nin*nhidden; 32 | net.c = reshape(w(1:mark1), nhidden, nin); 33 | if strcmp(net.actfn, 'gaussian') 34 | mark2 = mark1 + nhidden; 35 | net.wi = reshape(w(mark1+1:mark2), 1, nhidden); 36 | else 37 | mark2 = mark1; 38 | net.wi = []; 39 | end 40 | mark3 = mark2 + nhidden*nout; 41 | net.w2 = reshape(w(mark2+1:mark3), nhidden, nout); 42 | mark4 = mark3 + nout; 43 | net.b2 = reshape(w(mark3+1:mark4), 1, nout); -------------------------------------------------------------------------------- /rosegrad.m: -------------------------------------------------------------------------------- 1 | function g = rosegrad(x) 2 | %ROSEGRAD Calculate gradient of Rosenbrock's function. 3 | % 4 | % Description 5 | % G = ROSEGRAD(X) computes the gradient of Rosenbrock's function at 6 | % each row of X, which should have two columns. 7 | % 8 | % See also 9 | % DEMOPT1, ROSEN 10 | % 11 | 12 | % Copyright (c) Ian T Nabney (1996-2001) 13 | 14 | % Return gradient of Rosenbrock's test function 15 | 16 | nrows = size(x, 1); 17 | g = zeros(nrows,2); 18 | 19 | g(:,1) = -400 * (x(:,2) - x(:,1).^2) * x(:,1) - 2 * (1 - x(:,1)); 20 | g(:,2) = 200 * (x(:,2) - x(:,1).^2); 21 | -------------------------------------------------------------------------------- /rosen.m: -------------------------------------------------------------------------------- 1 | function y = rosen(x) 2 | %ROSEN Calculate Rosenbrock's function. 3 | % 4 | % Description 5 | % Y = ROSEN(X) computes the value of Rosenbrock's function at each row 6 | % of X, which should have two columns. 7 | % 8 | % See also 9 | % DEMOPT1, ROSEGRAD 10 | % 11 | 12 | % Copyright (c) Ian T Nabney (1996-2001) 13 | 14 | % Calculate value of Rosenbrock's function: x should be nrows by 2 columns 15 | 16 | y = 100 * ((x(:,2) - x(:,1).^2).^2) + (1.0 - x(:,1)).^2; 17 | -------------------------------------------------------------------------------- /somfwd.m: -------------------------------------------------------------------------------- 1 | function [d2, win_nodes] = somfwd(net, x) 2 | %SOMFWD Forward propagation through a Self-Organising Map. 3 | % 4 | % Description 5 | % D2 = SOMFWD(NET, X) propagates the data matrix X through a SOM NET, 6 | % returning the squared distance matrix D2 with dimension NIN by 7 | % NUM_NODES. The $i$th row represents the squared Euclidean distance 8 | % to each of the nodes of the SOM. 9 | % 10 | % [D2, WIN_NODES] = SOMFWD(NET, X) also returns the indices of the 11 | % winning nodes for each pattern. 12 | % 13 | % See also 14 | % SOM, SOMTRAIN 15 | % 16 | 17 | % Copyright (c) Ian T Nabney (1996-2001) 18 | 19 | % Check for consistency 20 | errstring = consist(net, 'som', x); 21 | if ~isempty(errstring) 22 | error(errstring); 23 | end 24 | 25 | % Turn nodes into matrix of centres 26 | nodes = (reshape(net.map, net.nin, net.num_nodes))'; 27 | % Compute squared distance matrix 28 | d2 = dist2(x, nodes); 29 | % Find winning node for each pattern: minimum value in each row 30 | [w, win_nodes] = min(d2, [], 2); 31 | -------------------------------------------------------------------------------- /sompak.m: -------------------------------------------------------------------------------- 1 | function [c] = sompak(net) 2 | %SOMPAK Combines node weights into one weights matrix. 3 | % 4 | % Description 5 | % C = SOMPAK(NET) takes a SOM data structure NET and combines the node 6 | % weights into a matrix of centres C where each row represents the node 7 | % vector. 8 | % 9 | % The ordering of the parameters in W is defined by the indexing of the 10 | % multi-dimensional array NET.MAP. 11 | % 12 | % See also 13 | % SOM, SOMUNPAK 14 | % 15 | 16 | % Copyright (c) Ian T Nabney (1996-2001) 17 | 18 | errstring = consist(net, 'som'); 19 | if ~isempty(errstring) 20 | error(errstring); 21 | end 22 | % Returns map as a sequence of row vectors 23 | c = (reshape(net.map, net.nin, net.num_nodes))'; 24 | -------------------------------------------------------------------------------- /somunpak.m: -------------------------------------------------------------------------------- 1 | function net = somunpak(net, w) 2 | %SOMUNPAK Replaces node weights in SOM. 3 | % 4 | % Description 5 | % NET = SOMUNPAK(NET, W) takes a SOM data structure NET and weight 6 | % matrix W (each node represented by a row) and puts the nodes back 7 | % into the multi-dimensional array NET.MAP. 8 | % 9 | % The ordering of the parameters in W is defined by the indexing of the 10 | % multi-dimensional array NET.MAP. 11 | % 12 | % See also 13 | % SOM, SOMPAK 14 | % 15 | 16 | % Copyright (c) Ian T Nabney (1996-2001) 17 | 18 | errstring = consist(net, 'som'); 19 | if ~isempty(errstring) 20 | error(errstring); 21 | end 22 | % Put weights back into network data structure 23 | net.map = reshape(w', [net.nin net.map_size]); -------------------------------------------------------------------------------- /xor.dat: -------------------------------------------------------------------------------- 1 | nin 2 2 | nout 1 3 | ndata 12 4 | 1 0 1 5 | 0 1 1 6 | 0 0 0 7 | 1 1 0 8 | 1 0 1 9 | 0 1 1 10 | 0 0 0 11 | 1 1 0 12 | 1 0 1 13 | 0 1 1 14 | 0 0 0 15 | 1 1 0 16 | --------------------------------------------------------------------------------