天天看点

MATLAB神经网络学习入门之BP神经网络参考资源BP神经网络简介代码

这里写自定义目录标题

  • 参考资源
  • BP神经网络简介
  • 代码

参考资源

https://www.bilibili.com/video/BV1Hx411M7Qw?p=4

BP神经网络简介

https://www.jianshu.com/p/9037890c9b65

代码

%%
%BP神经网络
%有导师学习神经网络
%前向神经网络(误差反向传播)
%要求激活函数可导/可微分
%学习算法:1. propagation 2. weight update--梯度下降
%数据归一化?(可以应用到Julien项目的数据中)
%newff-- create feed-forward backpropagation network
%train-- train neural network
%sim-- simulate neural network
%% 随机产生训练集和测试集
temp=randperm(size(features,1));
P_train=features(temp(1:300),:)';
T_train=activity(temp(1:300),:)';
%测试集
P_test=features(temp(301:end),:)';
T_test=activity(temp(301:end),:)';
N=size(P_test,2);
%% 数据归一化
[p_train,ps_input]=mapminmax(P_train,0,1);
p_test=mapminmax('apply',P_test,ps_input);

[t_train,ps_output]=mapminmax(T_train,0,1);
%% BP神经网络创建,训练及仿真测试
%创建网络
net=newff(p_train,t_train,9);

%设置训练参数
net.trainParam.epochs=1000;
net.trainParam.goal=1e-3;
net.trainParam.lr=0.01;

%训练网络
net=train(net,p_train,t_train);

%仿真测试
t_sim=sim(net,p_test);

%数据反归一化
T_sim=mapminmax('reverse',t_sim,ps_output);
%% 性能评价
%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'];
%% 绘图
figure
plot(1:N,T_test,'b:*',1:N,T_sim,'r-o')
legend('真实值','预测值')
xlabel('预测样本')
ylabel('activity')
string={'测试集人类行为预测结果对比';[]};
title(string)
           

继续阅读