Training a multilayer Neural Network with XOR training set
Show older comments
I'm trying to understand neural networks by training one with XOR training data. If I use training data of "and" or "or" functions neural network works fine. On the other hand it does not achieve satisfactory results with XOR, although there is a hidden layer with non-linear sigmoid activation function.
Can someone take a look at the code and guide me?
Thank you
clear all;
close all;
clc;
training_set = [ 0 0 0;
0 1 1;
1 0 1;
1 1 0];
[row_set column_set] = size(training_set);
y_test = ones(row_set,1);
w = 0.01*randn(3,2); [row column]=size(w);
delta_w = ones(3,2);
v = 0.01*randn(3,1);
delta_v = ones(3,1);
z = ones(3,1);
n = 1;
iteration = 0;
while(iteration < 1000)
i = randint(1,1,[1 row_set]);
x1 = training_set(i,1);
x2 = training_set(i,2);
x = [1 ;x1 ;x2];
for h=1:1:column
z(h+1) = sigmoid(w(:,h)'*x); % Computes hidden layer
%z(h+1) = myStep(w(:,h)'*x);
end
%y = myStep(v'*z); % Computes output from hidden layer
y = sigmoid(v'*z);
delta_v = n*(training_set(i,column_set)-y)*z;
for h=1:1:column
delta_w(:,h) = n*((training_set(i,column_set)-y)*v(h+1))*z(h+1)*(1-z(h+1))*x;
end
w = delta_w + w;
v = delta_v + v;
iteration = iteration + 1;
end
for i=1:1:length(training_set)
x1 = training_set(i,1);
x2 = training_set(i,2);
x = [1 ;x1 ;x2];
for h=1:1:column
z(h+1) = sigmoid(w(:,h)'*x); % Computes hidden layer
end
if (sigmoid(v'*z) > 0.5) y_test(i) =1; else y_test(i)=0; end;
%y_test(i) = sigmoid(v'*z);
end
if (y_test == training_set(:,column_set))
'Correct match!'
else
'Non-Correct match!'
end
Answers (1)
Selim Özel
on 3 Feb 2012
0 votes
6 Comments
Tounfos
on 12 May 2020
I want help. Can I see the full code you modified?
Haslinda Hassan
on 14 Jan 2021
i need help... can u send me the full coding
FRANKLIN VACA PIÑA
on 26 May 2021
please, can you help me?
Ahmed Atef
on 7 Jun 2021
I need help
can you send me the full coding,please.
im a student , and i have an exam
so, i need to understand the code.
samira sorayaasa
on 3 Oct 2021
Hello I need the code as well.
Ghiordy Ferney Contreras Contreras
on 17 Jun 2022
Can you send me your code? I would like to check.
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!