title: My simplified neural network can't learn XOR gate
body: <p>I'm trying to validate the results of the book: <a href="http://www.academia.dk/BiologiskAntropologi/Epidemiologi/DataMining/Artificial_Intelligence-A_Guide_to_Intelligent_Systems.pdf" rel="nofollow noreferrer">'Artificial Intelligence: A guide to Intelligent Systems 2nd Edition' by Michael Negnevitsky</a>, pages 181-183, on the neural network XOR gate.</p>
  
  <p>The algorithm is straightforward and the book claims the NN to learn in 224 epochs or 896 iterations. It shows the first iteration calculated manually and my program calculates the same values. But here's where the similarities stop.</p>
  
  <p>I can't  get it to perform anywhere near as good as shown in Figure 6.11 of the book. The network in the book (Figure 6.10) is shown in the figures below, followed by my MATLAB code.</p>
  
  <p><strong>Network:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/S52yR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S52yR.png" alt="Network"></a>
  <br> <sup><strong><em>Figure 6.10</strong> Three-layer network for solving the Exclusive-OR operation</em></sup></p>
  
  <p><strong>Learning curve for operation Exclusive-OR:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/RrCYe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RrCYe.png" alt="LearningCurve"></a>
  <br> <sup><strong><em>Figure 6.11</strong> Learning curve for operation Exclusive-OR</em></sup></p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>close all; clear; clc;
  alpha = 0.1; % Learning rate parameter
  
  % Initial conditions in the book
  w13 = 0.5;
  w14 = 0.9;
  w23 = 0.4;
  w24 = 1.0;
  
  w35 = -1.2;
  w45 = 1.1;
  
  t3 = 0.8;
  t4 = -0.1;
  t5 = 0.3;
  cost = zeros(1,4);
  
  X = [1 0 1 0;
       1 1 0 0];
  
  Y = [0 1 1 0];
  
  for epoch=1:250
      for i=1:4
          x1 = X(1,i);
          x2 = X(2,i);
          yd = Y(i);
  
          y3 = logsig(x1 * w13 + x2 * w23 - t3); % neuron 3 output
          y4 = logsig(x1 * w14 + x2 * w24 - t4); % neuron 4 output
  
          y5 = logsig(y3 * w35 + y4 * w45 - t5); % network output
  
          e = yd - y5; % error
          cost(epoch,i) = e^2;
          delta5 = y5 * (1 - y5) * e;
  
          % output neuron weight differentials
          dw35 = alpha * y3 * delta5;
          dw45 = alpha * y4 * delta5;
          dt5 = alpha * (-1) * delta5;
  
          delta3 = y3 * (1 - y3) * delta5 * w35;
          delta4 = y4 * (1 - y4) * delta5 * w45;
          % Hidden neuron weight differentials
          dw13 = alpha * x1 * delta3;
          dw23 = alpha * x2 * delta3;
  
          dt3 = alpha * (-1) * delta3;
  
          dw14 = alpha * x1 * delta4;
          dw24 = alpha * x2 * delta4;
  
          dt4 = alpha * (-1) * delta4;
  
          % Weight updates
          w13 = w13 + dw13;
          w14 = w14 + dw14;
          w23 = w23 + dw23;
          w24 = w24 + dw24;
          w35 = w35 + dw35;
          w45 = w45 + dw45;
  
          t3 = t3 + dt3;
          t4 = t4 + dt4;
          t5 = t5 + dt5;
      end
      if(sum(cost,2) &lt; 0.001)
          break
      end
  end
  
  semilogy(1:250, sum(cost,2))
  %% Test
  for i=1:4
      x1 = X(1,i);
      x2 = X(2,i);
      yd = Y(i);
  
      y3 = logsig(x1 * w13 + x2 * w23 - t3);
      y4 = logsig(x1 * w14 + x2 * w24 - t4);
  
      y5 = logsig(y3 * w35 + y4 * w45 - t5);
  
      disp(['         YD      Y'])
      disp([yd y5])
  end
  </code></pre>
  
  <p>I'm hoping someone sees what I'm doing wrong. Or, in an unlikely event, confirm that the results of the book are not possible with the information given (I doubt it, because according to what I've found, a 2-2-1 NN should be able to learn XOR easily). I just want to have a very simple 2-2-1 neural network with minimal code. </p>