人工神经元模型
S型函数(Sigmoid)
双极S型函数
神经网络可以分为哪些?
按照连接方式,可以分为:前向神经网络 vs. 反馈(递归)神经网络
按照学习方式,可以分为:有导师学习神经网络 vs. 无导师学习神经网络
按照实现功能,可以分为:拟合(回归)神经网络 vs. 分类神经网络
BP神经网络概述
Backpropagation is a common method of teaching artificial neural networks how to perform a given task.
It is a supervised learning method, and is a generalization of the delta rule. It requires a teacher that knows, or can calculate, the desired output for any input in the training set.
Backpropagation requires that the activation function used by the artificial neurons (or "nodes") be differentiable.
数据归一化
什么是归一化?
将数据映射到[0, 1]或[-1, 1]区间或其他的区间。
为什么要归一化?
输入数据的单位不一样,有些数据的范围可能特别大,导致的结果是神经网络收敛慢、训练时间长。
数据范围大的输入在模式分类中的作用可能会偏大,而数据范围小的输入作用就可能会偏小。
由于神经网络输出层的激活函数的值域是有限制的,因此需要将网络训练的目标数据映射到激活函数的值域。例如神经网络的输出层若采用S形激活函数,由于S形函数的值域限制在(0,1),也就是说神经网络的输出只能限制在(0,1),所以训练数据的输出就要归一化到[0,1]区间。
S形激活函数在(0,1)区间以外区域很平缓,区分度太小。例如S形函数f(X)在参数a=1时,f(100)与f(5)只相差0.0067。
归一化算法
y = ( x - min )/( max - min )
y = 2 * ( x - min ) / ( max - min ) - 1
程序
%% I. 清空环境变量 clear all clc %% 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. 数据归一化 [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); %% IV. BP神经网络创建、训练及仿真测试 %% % 1. 创建网络 net = newff(p_train,t_train,9); %% % 2. 设置训练参数 net.trainParam.epochs = 1000; net.trainParam.goal = 1e-3; net.trainParam.lr = 0.01; %% % 3. 训练网络 net = train(net,p_train,t_train); %% % 4. 仿真测试 t_sim = sim(net,p_test); %% % 5. 数据反归一化 T_sim = mapminmax('reverse',t_sim,ps_output); %% V. 性能评价 %% % 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'] %% VI. 绘图 figure plot(1:N,T_test,'b:*',1:N,T_sim,'r-o') legend('真实值','预测值') xlabel('预测样本') ylabel('辛烷值') string = {'测试集辛烷值含量预测结果对比';['R^2=' num2str(R2)]}; title(string)
运行结果: