Classification of XOR problem with an exact RBFN

Neural Networks course (practical examples) © 2012 Primoz Potocnik

PROBLEM DESCRIPTION: 2 groups of linearly inseparable data (A,B) are defined in a 2-dimensional input space. The task is to define a neural network for solving the XOR classification problem.

Contents

Create input data

close all, clear all, clc, format compact

% number of samples of each cluster
K = 100;
% offset of clusters
q = .6;
% define 2 groups of input data
A = [rand(1,K)-q rand(1,K)+q;
     rand(1,K)+q rand(1,K)-q];
B = [rand(1,K)+q rand(1,K)-q;
     rand(1,K)+q rand(1,K)-q];
% plot data
plot(A(1,:),A(2,:),'k+',B(1,:),B(2,:),'b*')
grid on
hold on

Define output coding

% coding (+1/-1) for 2-class XOR problem
a = -1;
b =  1;

Prepare inputs & outputs for network training

% define inputs (combine samples from all four classes)
P = [A B];
% define targets
T = [repmat(a,1,length(A)) repmat(b,1,length(B))];

Create an exact RBFN

% choose a spread constant
spread = 1;
% create a neural network
net    = newrbe(P,T,spread);

% view network
view(net)
Warning: Rank deficient, rank = 124, tol =  8.881784e-14. 

Evaluate network performance

% simulate a network on training data
Y = net(P);

% calculate [%] of correct classifications
correct = 100 * length(find(T.*Y > 0)) / length(T);
fprintf('\nSpread          = %.2f\n',spread)
fprintf('Num of neurons  = %d\n',net.layers{1}.size)
fprintf('Correct class   = %.2f %%\n',correct)

% plot targets and network response
figure;
plot(T')
hold on
grid on
plot(Y','r')
ylim([-2 2])
set(gca,'ytick',[-2 0 2])
legend('Targets','Network response')
xlabel('Sample No.')
Spread          = 1.00
Num of neurons  = 400
Correct class   = 100.00 %

Plot classification result

% generate a grid
span    = -1:.025:2;
[P1,P2] = meshgrid(span,span);
pp      = [P1(:) P2(:)]';
% simualte neural network on a grid
aa      = sim(net,pp);

% plot classification regions based on MAX activation
figure(1)
ma = mesh(P1,P2,reshape(-aa,length(span),length(span))-5);
mb = mesh(P1,P2,reshape( aa,length(span),length(span))-5);
set(ma,'facecolor',[1 0.2 .7],'linestyle','none');
set(mb,'facecolor',[1 1.0 .5],'linestyle','none');
view(2)

Plot RBFN centers

plot(net.iw{1}(:,1),net.iw{1}(:,2),'gs')