0

I am trying to make a simple XOR gate in Matlab just to demonstrate a feed forward network but I am having trouble in getting the output to match my target. I'm pretty new to ANN in Matlab so any help would be greatly appreciated. Here is my simple code:

% XOR gate
x = [0 0 1 1; 0 1 0 1] ;
t = [0,1,1,0];
net = feedforwardnet(2);
net = train(net,x,t);
y = net(x)
disp("Weights:")
disp(net.IW{1})
disp("Bias:")
disp(net.b{1})

The last bit is just used so I can see the weights and biases used. For some context, this is just from a problem sheet where previously I created an OR and AND gate for example but these are linearly separable so I could do it with a single perceptron! This XOR gate has me quite confused tho!

Thanks in advance for any help :D

EDIT: Just an update for anyone coming across this who isn't helped by the other posts provided, I managed to solve the XOR gate using a radial basis function (newrb in Matlab) and it gave a perfect results from the given target. :)

Lushawn
  • 522
  • 1
  • 3
  • 17
  • This question sounds familiar. Have a look here [4 years ago] (https://stackoverflow.com/questions/30163573/xor-with-neural-networks-matlab) or [6 years ago](https://stackoverflow.com/questions/20190471/how-to-desig-mlp-neural-network-in-matlab) or [9 years ago](https://stackoverflow.com/questions/3563874/i-want-a-x-or-code-for-neural-network-in-matlab/3566553#3566553) or [10 years ago] (https://stackoverflow.com/questions/2417716/neural-network-in-matlab)... – max Apr 05 '20 at 14:22
  • 1
    Does this answer your question? [Neural network in MATLAB](https://stackoverflow.com/questions/2417716/neural-network-in-matlab) – max Apr 05 '20 at 14:23

1 Answers1

1

try this working code: x represent the training examples y is the target I add an additional hidden layer to the network with two neurons you must first configure the net before you training it. the network uses sigmoid function as activation function.

x = [0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1; 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1];
y =[0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0];
net = feedforwardnet([2 2]);
net = configure(net,x,y);
net.layers{3}.transferFcn = 'logsig';
[net,tr] = train(net,x,y);
net(x)
  • 1
    An answer is much more useful if you explain what the code does, and how that solves the problem in the question. – Cris Luengo Apr 05 '20 at 17:51