% xor_2hid.m % 2/21/09: modified for new Version 6 Toolbox % Date: 2/3/03 Modified xor_1hid.m for XOR % Author: G. Dempsey, Bradley Univ.- Electrical Engineering % redefine pattern/targets for tansig neurons close all clc clear % PROBLEM DEFINITION %=================== y1=[-1 -1]'; y2=[-1 1]'; y3=[1 -1]'; y4=[1 1]'; P=[y1 y2 y3 y4]; % Define targets. T=[-0.9 0.9 0.9 -0.9]; % INITIALIZE NETWORK ARCHITECTURE %================================ % Set input vector size R, layer sizes S1, S2 & S3. S1=2; % 2 hidden neurons, 1st layer S2=2; % 2 hidden neurons, 2nd layer S3=1; % single nonlinear output neuron R=2; % number of input components, note parameter % not used by training fcn, but good documentation % Initialize weights and biases. Nyguyen/Widrow method is the default % initialize the network with single command % function newff for Version 5.0 Toolbox: enclosed in *********** %**************************************************************** %net=newff([-1 1;-1 1],[S1,S2,S3],{'tansig','tansig','tansig'},'traingdx'); %**************************************************************** % initialize the network with single command % function newff for Version 6 Toolbox: enclosed in *********** %********************************************************************* net=newff(P,T,[S1,S2],{'tansig','tansig','tansig'},'traingdx'); % BP with adaptive learning and momentum net.divideFcn = ''; % don't divide input data into test and validation data net.inputs{1}.processFcns={}; % don't perform advanced preprocessing net.outputs{2}.processFcns={}; % don't perform advanced postprocessing %net.trainParam.showWindow = false; % this will disable TRAIN GUI %net.trainParam.showCommandLine = true; % this will enable status in command window %********************************************************************** % save a copy of the weights/biases from newff W10=net.IW{1,1}; B10=net.b{1}; W20=net.LW{2,1}; B20=net.b{2}; W30=net.LW{3,2}; B30=net.b{3}; % Output INITIAL ANN Output %==================================== A=sim(net,P) disp('press any key to start training') pause; % TRAIN THE NETWORK %================== % TRAINING PARAMETERS net.trainParam.show=50; % display update net.trainParam.lr=0.01; % learning rate net.trainParam.epochs=2000; % max epochs net.trainParam.goal=0.02; % error goal net.trainParam.lr_inc=1.05; % lr increase net.trainParam.lr_dec=0.7; % lr decrease net.trainParam.mc=0.95; % momentum net.trainParam.max_perf_inc=1.04; % error ratio check for weight discard % Now train the ANN [net,tr]=train(net,P,T); disp('Press key to see final results') pause % summarize fprintf('\nFINAL NETWORK VALUES:\n') W1=net.IW{1,1} % input to hidden layer weights B1=net.b{1} % 1st hidden neuron biases W2=net.LW{2,1} % 1st hidden to 2nd hidden layer weights B2=net.b{2} % 2nd hidden neuron biases W3=net.LW{3,2} % 2nd hidden to output B3=net.b{3} % output neuron bia A=sim(net,P) % show ANN output with converged weights fprintf('Trained for %.0f epochs.\n',max(tr.epoch)) fprintf('Mean square error goal was %g.\n',net.trainParam.goal) fprintf('Final mean square error is %g.\n',tr.perf(max(tr.epoch+1))); fprintf('Trained network operates: '); if tr.perf(max(tr.epoch+1))< net.trainParam.goal disp('It works!') else disp('It Failed!') end disp('press any key to continue') pause; % PLOT Adaptive learning rate CURVE %================= figure disp('plot learning rate curve'); plot(tr.lr); title('Adaptive Learning Rate Versus Epochs'); xlabel('Epochs'); ylabel('Learning Rate');