当前位置:网站首页>MATLAB小技巧(24)RBF,GRNN,PNN-神经网络
MATLAB小技巧(24)RBF,GRNN,PNN-神经网络
2022-07-03 02:24:00 【mozun2020】
MATLAB小技巧(24)RBF,GRNN,PNN-神经网络
前言
MATLAB进行图像处理相关的学习是非常友好的,可以从零开始,对基础的图像处理都已经有了封装好的许多可直接调用的函数,这个系列文章的话主要就是介绍一些大家在MATLAB中常用一些概念函数进行例程演示!
RBF神经网络一共分为三层,第一层为输入层即Input Layer,由信号源节点组成;第二层为隐藏层即图中中间的黄球,隐藏层中神经元的变换函数即径向基函数是对中心点径向对称且衰减的非负线性函数,该函数是局部响应函数。因为是局部相应函数,所以一般要根据具体问题设置相应的隐藏层神经元个数;第三层为输出层,是对输入模式做出的响应,输出层是对线性权进行调整,采用的是线性优化策略,因而学习速度较快。
GRNN广义回归神经网络是径向基神经网络的一种,GRNN具有很强的非线性映射能力和学习速度,比RBF具有更强的优势,网络最后普收敛于样本量集聚较多的优化回归,样本数据少时,预测效果很好,还可以处理不稳定数据。虽然GRNN看起来没有径向基精准,但实际在分类和拟合上,特别是数据精准度比较差的时候有着很大的优势。
PNN(Product-based Neural Network)是在2016年提出的用于计算CTR问题的深度神经网络模型,PNN的网络结构对传统的FNN(Feedforward Neural Network)网络结构做了一些优化,使得其能够更适合处理CTR问题。
径向基神经元和线性神经元可以建立广义回归神经网络GRNN,它是RBF网络的一种变化形式,经常用于函数逼近。在某些方面比RBF网络更具优势。径向基神经元和竞争神经元还可以组成概率神经网络PNN。PNN也是RBF的一种变化形式,结构简单训练快捷,特别适合于模式分类问题的解决。两个仿真示例分享给大家,MATLAB版本为MATLAB2015b。
一. MATLAB仿真一
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%功能:矩阵分析--RBF神经网络
%环境:Win7,Matlab2015b
%Modi: C.S
%时间:2022-06-27
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% I. 清空环境变量
clear all
clc
tic
%% II. 训练集/测试集产生
%%
% 1. 导入数据
load spectra_data.mat
%%
% 2. 随机产生训练集和测试集
temp = randperm(size(NIR,1));
% 训练集——50个样本
P_train = NIR(temp(1:50),:)';
T_train = octane(temp(1:50),:)';
% 测试集——10个样本
P_test = NIR(temp(51:end),:)';
T_test = octane(temp(51:end),:)';
N = size(P_test,2);
%% III. RBF神经网络创建及仿真测试
%%
% 1. 创建网络
net = newrbe(P_train,T_train,30);
%%
% 2. 仿真测试
T_sim = sim(net,P_test);
%% IV. 性能评价
%%
% 1. 相对误差error
error = abs(T_sim - T_test)./T_test;
%%
% 2. 决定系数R^2
R2 = (N * sum(T_sim .* T_test) - sum(T_sim) * sum(T_test))^2 / ((N * sum((T_sim).^2) - (sum(T_sim))^2) * (N * sum((T_test).^2) - (sum(T_test))^2));
%%
% 3. 结果对比
result = [T_test' T_sim' error']
%% V. 绘图
figure
plot(1:N,T_test,'b:*',1:N,T_sim,'r-o')
legend('真实值','预测值')
xlabel('预测样本')
ylabel('辛烷值')
string = {
'测试集辛烷值含量预测结果对比';['R^2=' num2str(R2)]};
title(string)
toc
点击“运行”,得到仿真结果如下:
result =
88.5500 87.2278 0.0149
84.7000 83.8908 0.0096
88.8500 89.6147 0.0086
88.2500 88.0039 0.0028
88.7000 83.4154 0.0596
87.9000 86.0295 0.0213
87.0500 87.5730 0.0060
85.3000 86.1662 0.0102
88.4500 85.4190 0.0343
88.0000 86.5528 0.0164
时间已过 2.260245 秒。
二. MATLAB仿真二
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%功能:GRNN_PNN神经网络
%环境:Win7,Matlab2015b
%Modi: C.S
%时间:2022-06-27
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% I. 清空环境变量
clear all
clc
tic
%% II. 训练集/测试集产生
%%
% 1. 导入数据
load iris_data.mat
%%
% 2 随机产生训练集和测试集
P_train = [];
T_train = [];
P_test = [];
T_test = [];
for i = 1:3
temp_input = features((i-1)*50+1:i*50,:);
temp_output = classes((i-1)*50+1:i*50,:);
n = randperm(50);
% 训练集——120个样本
P_train = [P_train temp_input(n(1:40),:)'];
T_train = [T_train temp_output(n(1:40),:)'];
% 测试集——30个样本
P_test = [P_test temp_input(n(41:50),:)'];
T_test = [T_test temp_output(n(41:50),:)'];
end
%% III. 模型建立
result_grnn = [];
result_pnn = [];
time_grnn = [];
time_pnn = [];
for i = 1:4
for j = i:4
p_train = P_train(i:j,:);
p_test = P_test(i:j,:);
%%
% 1. GRNN创建及仿真测试
t = cputime;
% 创建网络
net_grnn = newgrnn(p_train,T_train);
% 仿真测试
t_sim_grnn = sim(net_grnn,p_test);
T_sim_grnn = round(t_sim_grnn);
t = cputime - t;
time_grnn = [time_grnn t];
result_grnn = [result_grnn T_sim_grnn'];
%%
% 2. PNN创建及仿真测试
t = cputime;
Tc_train = ind2vec(T_train);
% 创建网络
net_pnn = newpnn(p_train,Tc_train);
% 仿真测试
Tc_test = ind2vec(T_test);
t_sim_pnn = sim(net_pnn,p_test);
T_sim_pnn = vec2ind(t_sim_pnn);
t = cputime - t;
time_pnn = [time_pnn t];
result_pnn = [result_pnn T_sim_pnn'];
end
end
%% IV. 性能评价
%%
% 1. 正确率accuracy
accuracy_grnn = [];
accuracy_pnn = [];
time = [];
for i = 1:10
accuracy_1 = length(find(result_grnn(:,i) == T_test'))/length(T_test);
accuracy_2 = length(find(result_pnn(:,i) == T_test'))/length(T_test);
accuracy_grnn = [accuracy_grnn accuracy_1];
accuracy_pnn = [accuracy_pnn accuracy_2];
end
%%
% 2. 结果对比
result = [T_test' result_grnn result_pnn];
accuracy = [accuracy_grnn;accuracy_pnn]
time = [time_grnn;time_pnn]
%% V. 绘图
figure(1)
plot(1:30,T_test,'bo',1:30,result_grnn(:,4),'r-*',1:30,result_pnn(:,4),'k:^')
grid on
xlabel('测试集样本编号')
ylabel('测试集样本类别')
string = {
'测试集预测结果对比(GRNN vs PNN)';['正确率:' num2str(accuracy_grnn(4)*100) '%(GRNN) vs ' num2str(accuracy_pnn(4)*100) '%(PNN)']};
title(string)
legend('真实值','GRNN预测值','PNN预测值')
figure(2)
plot(1:10,accuracy(1,:),'r-*',1:10,accuracy(2,:),'b:o')
grid on
xlabel('模型编号')
ylabel('测试集正确率')
title('10个模型的测试集正确率对比(GRNN vs PNN)')
legend('GRNN','PNN')
figure(3)
plot(1:10,time(1,:),'r-*',1:10,time(2,:),'b:o')
grid on
xlabel('模型编号')
ylabel('运行时间(s)')
title('10个模型的运行时间对比(GRNN vs PNN)')
legend('GRNN','PNN')
toc
点击“运行”,得到仿真结果如下:
accuracy =
0.3667 0.4000 1.0000 1.0000 0.3333 1.0000 1.0000 1.0000 1.0000 0.7000
0.6333 0.7667 1.0000 1.0000 0.4667 0.9333 1.0000 1.0000 1.0000 0.9667
time =
1.4352 0.1404 0.0780 0.0624 0.0468 0.0624 0.0468 0.0468 0.0468 0.0468
0.1716 0.0624 0.0624 0.0624 0.0468 0.0468 0.0624 0.0468 0.0624 0.0468
时间已过 3.570447 秒。
三. 小结
RBF,GRNN,PNN,三种神经网络进行预测分析的示例仿真,其实在自己的专栏《MATLAB 神经网络43个案例分析》中也有介绍到三种神经网络,感兴趣的同学也可以移步到专栏,链接在文末。每天学一个MATLAB小知识,大家一起来学习进步阿!
边栏推荐
- Kotlin middle process understanding and Practice (II)
- How do it students find short-term internships? Which is better, short-term internship or long-term internship?
- easyPOI
- Y54. Chapter III kubernetes from introduction to mastery -- ingress (27)
- [shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)
- [Flutter] dart: class; abstract class; factory; Class, abstract class, factory constructor
- 微服务组件Sentinel (Hystrix)详细分析
- Cfdiv2 fixed point guessing- (interval answer two points)
- Producer consumer model based on thread pool (including blocking queue)
- [advanced ROS] Lesson 6 recording and playback in ROS (rosbag)
猜你喜欢
Servlet中数据传到JSP页面使用el表达式${}无法显示问题
Restcloud ETL cross database data aggregation operation
The use of Flink CDC mongodb and the implementation of Flink SQL parsing complex nested JSON data in monggo
Use go language to realize try{}catch{}finally
udp接收队列以及多次初始化的测试
[advanced ROS] Lesson 6 recording and playback in ROS (rosbag)
面试项目技术栈总结
Job object of collaboration in kotlin
SPI mechanism
Stm32f407 ------- IIC communication protocol
随机推荐
[Yu Yue education] reference materials of chemical experiment safety knowledge of University of science and technology of China
Gbase 8C system table PG_ class
返回一个树形结构数据
CFdiv2-Fixed Point Guessing-(區間答案二分)
[tutorial] chrome turns off cross domain policies CORS and samesite, and brings cookies across domains
awk从入门到入土(1)awk初次会面
面试八股文整理版
Restcloud ETL cross database data aggregation operation
基于线程池的生产者消费者模型(含阻塞队列)
UDP receive queue and multiple initialization test
5.文件操作
[shutter] bottom navigation bar implementation (bottomnavigationbar bottom navigation bar | bottomnavigationbaritem navigation bar entry | pageview)
Cancellation of collaboration in kotlin, side effects of cancellation and overtime tasks
GBase 8c触发器(三)
[codeforces] cf1338a - Powered addition [binary]
Simple understanding of SVG
苏世民:25条工作和生活原则
Javescript 0.1 + 0.2 = = 0.3 problem
Leetcode 183 Customers who never order (2022.07.02)
elastic stack