Training a multilayer Neural Network with XOR training set
5 views (last 30 days)
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
0 Comments
Answers (1)
See Also
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!