使用PNN进行数据分类

326 篇文章 2 订阅
85 篇文章 2 订阅

Classification of an XOR problem with a PNN

% ========================================================================
% PROBLEM DESCRIPTION:
% 4 clusters of data (A,B,C,D) are defined in a 2-dimensional input space.
% (A,C) and (B,D) clusters represent XOR classification problem.
% The task is to define a neural network for solving the XOR problem.
% ========================================================================
% NEURAL NETWORK course example by Primoz Potocnik, 2010.
% Optimized for MATLAB 2010b with Neural Network Toolbox 7.0
% ========================================================================
% 清除变量及清理环境
close all; clear; clc

Define 4 clusters of input data

% number of samples of each class
K = 100;
% define 4 clusters of input data
q = .6; % offset of classes
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 clusters
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

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

Prepare inputs & outputs for network training

% define inputs (combine samples from all four classes)
% size(P) = 2行 X 400列
P = [A B C D];
% define targets
% 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

% choose a spread constant
% 传播参数
spread = .5;
% create a neural network
% 创建网络
net = newpnn(P,ind2vec(T),spread);

% view network
view(net)

Evaluate network performance

% simulate RBFN on training data
% 模拟输出
Y = net(P);
% convert PNN outputs
% 结果转换为类标签
Y = vec2ind(Y);

% calculate [%] of correct classifications
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)

% plot targets and network response
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

% generate a grid
span = -1:.02:2;
[P1,P2] = meshgrid(span,span);
pp = [P1(:) P2(:)]';
% simualte PNN on a grid
aa = sim(net,pp);
aa = full(vec2ind(aa))-1.5; % convert to

% plot classification regions based on MAX activation
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 PNN centers
plot(net.iw{1}(:,1),net.iw{1}(:,2),'gs','linewidth',2)
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值