Neural Network Demos In matlab (you need the Neural Network Tool box ) nnd2n1 One-input neuron demonstration. nnd2n2 Two-input neuron demonstration. nnd4db Decision boundaries demonstration.+ nnd4pr Perceptron rule demonstration.+ nnd9sdq Steepest descent for quadratic function demonstration. nnd11nf Network function demonstration. (you can visualize function computed by a simple neural network as a function of the weights and biases) nnd11bc Backpropagation calculation demonstration (step by step backprop demo) nnd11fa Function approximation demonstration. nnd11gn Generalization demonstration (you can add more hidden neurons and/or make task more difficult) ---------------------------------- xorbp.m p = [0 0 1 1 ;0 1 0 1 ]; t=[0 1 1 0 ] %net=newff(p, t, 3, {'logsig', 'logsig'}, 'traingd'); % syntax for 7.1 %net=newff(p,t,3); % new syntax for 8.0 %net=feedforwardnet(3); % new syntax for 2012a net=patternnet(3); % specific for 1 of n outputs net.trainParam.epochs=1500; net.trainParam.lr = 0.8; net.trainParam.goal = 0.01; net.divideFcn=''; %%% We don't want to divide training set in this simple case %% line not needed for 8.0 net=train(net,p,t); %% not currently working for 8.0 a = sim(net,p) for comparison we can look at Nelder-Mead for the same network %% Optimal Fit of a Non-linear Function % This is a demonstration of the optimal fitting of a non-linear function to a % set of data. It uses FMINSEARCH, an implementation of the Nelder-Mead simplex% (direct search) algorithm, to minimize a nonlinear function of several % variables. % % Modified by VdS from program fitdemo by % Copyright 1984-2004 The MathWorks, Inc. % $Revision: 5.15.4.2 $ $Date: 2004/08/16 01:37:29 $ %% % First, create some sample data x = [0 0; 0 1; 1 0; 1 1]; y = [0 1 1 0]' %% % The goal is to fit a neural network with 13 non-linear parameters % to the XOR data: % % % To fit this function, we've create a function MYFITFUNSLOW. Given the % , FITFUN calculates the error in the % fit for this equation and updates the line (h). type neuralfitfun2 %% % Make a guess for initial estimate of w41 w43 w31 w32 b2 wv2 b4 % and invoke FMINSEARCH. It % minimizes the error returned from FITFUN by adjusting them. It returns the % final value of the params. % random initial small weights start=(rand(13,1)-.5); %start=[1,1,-2,100,1000,-300,0,-30,200,-400,100,-9,200] % good start val options = optimset('TolFun',0.01); % termination tolerance on error [estimated_d,pval,exitflag] = fminsearch(@(w) neuralfitfun2(w,x,y),start) pvec=estimated_d; z= sigm(pvec(10)*sigm(pvec(1)*x(:,1)+pvec(2)*x(:,2) +pvec(3))+pvec(11)*sigm(pvec(4)*x(:,1)+ pvec(5)*x(:,2)+(pvec(6)) + pvec(12)* sigm(pvec(7)*x(:,1)+pvec(8)*x(:,2)+pvec(9)) +pvec(13))) err=norm(z-y) function err = neuralfitfun2(w31w32b3w41w42b4w51w52b5,x,y) %FITFUN Used by FITDEMO. % FITFUN(lambda,t,y,handle) returns the error between the data and the values % computed by the current function of lambda. % % modified by VdS from % Copyright 1984-2002 The MathWorks, Inc. % $Revision: 5.8 $ $Date: 2002/04/15 03:36:42 $ pvec=w31w32b3w41w42b4w51w52b5 z= sigm(pvec(10)*sigm(pvec(1)*x(:,1)+pvec(2)*x(:,2) +pvec(3))+pvec(11)*sigm(pvec(4)*x(:,1)+ pvec(5)*x(:,2)+(pvec(6)) + pvec(12)* sigm(pvec(7)*x(:,1)+pvec(8)*x(:,2)+pvec(9)) +pvec(13))); eArr = norm(z-y) %set(gcf,'DoubleBuffer','on'); %set(handle,'ydata',z) %drawnow %pause(.04)