How to train feedforward network to solve XOR function

Illustration
Albert - 2021-07-14T10:44:48+00:00
Question: How to train feedforward network to solve XOR function

im new in matlab, please sorry if its stupid question. and sorry my english. trying to train feedforward network to solve XOR function 1 hidden layer with 2 neurons, other settings are default: TANSIG, Backprop, TRAINLM, LEARNGDM, MSE R2012b matlab version close all, clear all, clc, format compact p = [0 1 0 1 ; 0 0 1 1]; t = [0 1 1 0]; net = feedforwardnet(2,'trainlm'); net = train(net,p,t); a = net(p) ive tried this code, and tried 'nntool' and 'nnstart' too. its always seems like training algorithm splits 'p' set for 2 - training set, 1 - validation set, 1 - testing set as a result - network is training on partial data (2 pair of digits instead 4), and training process generates Validation done or Minimum gradient reached (1.00e-010) in very few iteration (1-10 iterations) and simulation shows that network untrained. Is my guess right (about splitting 'p' set)? how i can manually give validation data (input and output sets) to training algorithm? should i somehow expand 'p' and 't' sets, and then use divideblock? any other ideas?

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

 1.[ I N ] = size(x)   % [ 2 4 ]
   [ O N ] = size(t)   % [ 1 4 ]
   Neq = prod(size(t)  % 4 = No. of training equations

2. For tthis small data set it doesn't make sense to use data division for validation stopping. So,

net.divideFcn = 'dividetrain'; % or equivalently,  = ' ';

3. Since the No. of estimated weights for H hidden nodes is

 %Nw = (I+1)*O          = 3 for H=0
 %Nw = (I+1)*H+(H+1)*O  for H >0

the condition Neq >= Nw yields the following upper bound for H

Hub = (Neq-O)/(I+O+1)    % 3/4
which is only possible for H = 0 (no hidden layer). However from a 2-dimensional plot we know that it will take at least 2 hidden nodes to separate the "0" class diagonal corners [ 0 1; 0 1 ] from the "1" class diagonal corners [ 1 0 ; 0 1].
 
Subsequently, for H = 2, Nw = 9 > Neq = 4. Therefore, there will be an infinite number of solutions for the weights.
 net = patternnet(2);   % for classification
4. Choose MSEgoal so that the coefficient of determination ( or R^2, see wikipedia) is >= 0.99 . Then the model will represent at least 99% of the biased target variance:
 
 
 net.trainParam.goal = 0.01*var(t',1);
5. The success of the design depends on the placement of the random initial weights. Therefore it may be necessary to make Ntrials >= 10 separate designs (use a do loop).
 
6. When training the net use the extended output form
 [ net tr y e ] = train(net,x,t);
Then, everything you need to know, besides the output y and error e, can be obtained directly from the training structure tr.
 
7. It is STRONGLY recommended that somewhere along the line you should investigate the contents of tr.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!