% % BPXORB.M: XOR Problem (HW 13 part 3) % Batch mode training % % Author: G. Dempsey, Bradley University Electrical Engineering % Last code modification 2/21/09 % modification required for new version (6.0) of NN Toolbox % % A network, consisting of 2 inputs fed to a two-layer network, % is trained with backpropagation on a 4 pattern problem. % Layer 1 consists of 2 log-sigmoid neurons, while layer 2 % is made up of 1 log-sigmoid neuron. % %=================== clc; % clear console clear; % clear variables close all; % close all figure windows % Define input vectors. Y1 = [0 0]'; Y2 = [0 1]'; Y3 = [1 0]'; Y4 = [1 1]'; disp('display input and target patterns') P = [Y1 Y2 Y3 Y4] % Define the associated targets. T = [0 1 1 0]; T = [0.15 0.85 0.85 0.15] % overwrite ideal targets with practical values % % % INITIALIZE NETWORK ARCHITECTURE %================================ % Set input vector size R, layer sizes S1 & S2 S1 = 2; % two hidden neurons S2 = 1; % one output neuron R=2; % 2 components of input vector (documentation only) % initialize the network with single command % Initialize weights and biases. Nyguyen/Widrow method is the default % function newff for Version 5.0 Toolbox: enclosed in *********** %**************************************************************** %net=newff([0 1;0 1],[S1,S2],{'logsig','logsig'},'traingd'); %**************************************************************** % initialize the network with single command % function newff for Version 6 Toolbox: enclosed in *********** %********************************************************************* net=newff(P,T,S1,{'logsig','logsig'},'traingd'); 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 show training status in command window %********************************************************************** % override the initialized weight/biases from newff to match HW #13 net.IW{1}=[-6.8 2.8; 5.9 -5.1]; % input to hidden layer weights W10=net.IW{1}; % save copy net.b{1}=[5.3 -0.2]'; % hidden neuron biases B10=net.b{1}; % save copy net.LW{2}=[-2.5 0.8]; % hidden to output layer weights W20=net.LW{2}; % save copy net.b{2}=[1.4]; % output neuron bias B20=net.b{2}; % save copy % TRAINING PARAMETERS net.trainParam.show=50; net.trainParam.lr=0.5; net.trainParam.epochs=1000; net.trainParam.goal=0.02; net.performFcn='sse'; % Now train the ANN [net,tr]=train(net,P,T); % SUMMARIZE RESULTS %================== fprintf('\nINITIAL NETWORK VALUES:\n') W10 B10 W20 B20 fprintf('\nFINAL NETWORK VALUES:\n') W1=net.IW{1} % input to hidden layer weights B1=net.b{1} % hidden neuron biases W2=net.LW{2} % hidden to output layer weights B2=net.b{2} % output neuron bias fprintf('\nNeural Network Outputs:\n') A=sim(net,P) fprintf('Trained for %.0f epochs.\n',max(tr.epoch)) fprintf('Sum squared error goal was %g.\n',net.trainParam.goal) fprintf('Final sum squared 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