Classification of an XOR problem with a PNN
% 清除变量及清理环境
close all; clear; clc
Define 4 clusters of input data
K = 100;
q = .6;
A = [rand(1,K)-q; rand(1,K)+q];
B = [rand(1,K)+q; rand(1,K)+q];
C = [rand(1,K)+q; rand(1,K)-q];
D = [rand(1,K)-q; rand(1,K)-q];
plot(A(1,:),A(2,:),'k+')
hold on
grid on
plot(B(1,:),B(2,:),'b*')
plot(C(1,:),C(2,:),'k+')
plot(D(1,:),D(2,:),'b*')
Define output coding
a = 1;
c = 1;
b = 2;
d = 2;
Prepare inputs & outputs for network training
% size(P) = 2行 X 400列
P = [A B C D];
% size(T) = 1行 X 400列
T = [repmat(a,1,length(A)) repmat(b,1,length(B)) ...
repmat(c,1,length(C)) repmat(d,1,length(D)) ];
Create a PNN
% 传播参数
spread = .5;
% 创建网络
net = newpnn(P,ind2vec(T),spread);
view(net)
Evaluate network performance
% 模拟输出
Y = net(P);
% 结果转换为类标签
Y = vec2ind(Y);
correct = 100 * length(find(T==Y)) / length(T);
fprintf('\nSpread = %.2f\n',spread)
fprintf('Num of neurons = %d\n',net.layers{1}.size)
fprintf('Correct class = %.2f %%\n',correct)
figure;
plot(T')
ylim([0 3])
set(gca,'ytick',[0 1 2 3])
hold on
grid on
plot(Y','r')
legend('Targets','Network response')
xlabel('Sample No.')
Spread = 0.50
Num of neurons = 400
Correct class = 100.00 %
Plot classification result for the complete input space
span = -1:.02:2;
[P1,P2] = meshgrid(span,span);
pp = [P1(:) P2(:)]';
aa = sim(net,pp);
aa = full(vec2ind(aa))-1.5;
figure(1)
m = mesh(P1,P2,reshape(-aa,length(span),length(span))-5);
set(m,'facecolor',[1 0.2 .7],'linestyle','none');
hold on
view(2)
m = mesh(P1,P2,reshape(aa,length(span),length(span))-5);
set(m,'facecolor',[1 1.0 0.5],'linestyle','none');
Plot PNN centers on the classification result
plot(net.iw{1}(:,1),net.iw{1}(:,2),'gs','linewidth',2)