多输入多输出 | MATLAB实现PSO-BP粒子群优化BP神经网络多输入多输出
目录
预测效果
基本介绍
Matlab实现PSO-BP粒子群优化BP神经网络多输入多输出预测
1.data为数据集,10个输入特征,3个输出变量。
2.main.m为主程序文件。
3.命令窗口输出MBE、MAE和R2,可在下载区获取数据和程序内容。
程序设计
- 完整程序和数据下载方式:私信博主回复MATLAB实现PSO-BP粒子群优化BP神经网络多输入多输出。
clike
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 节点个数
inputnum = size(p_train, 1); % 输入层节点数
hiddennum = 5; % 隐藏层节点数
outputnum = size(t_train,1); % 输出层节点数
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 建立网络
net = newff(p_train, t_train, hiddennum);
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 设置训练参数
net.trainParam.epochs = 1000; % 训练次数
net.trainParam.goal = 1e-6; % 目标误差
net.trainParam.lr = 0.01; % 学习率
net.trainParam.showWindow = 0; % 关闭窗口
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 参数初始化
c1 = 4.494; % 学习因子
c2 = 4.494; % 学习因子
maxgen = 50; % 种群更新次数
sizepop = 5; % 种群规模
Vmax = 1.0; % 最大速度
Vmin = -1.0; % 最小速度
popmax = 1.0; % 最大边界
popmin = -1.0; % 最小边界
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 节点总数
numsum = inputnum * hiddennum + hiddennum + hiddennum * outputnum + outputnum;
for i = 1 : sizepop
pop(i, :) = rands(1, numsum); % 初始化种群
V(i, :) = rands(1, numsum); % 初始化速度
fitness(i) = fun(pop(i, :), hiddennum, net, p_train, t_train);
end
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 个体极值和群体极值
[fitnesszbest, bestindex] = min(fitness);
zbest = pop(bestindex, :); % 全局最佳
gbest = pop; % 个体最佳
fitnessgbest = fitness; % 个体最佳适应度值
BestFit = fitnesszbest; % 全局最佳适应度值
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%% 迭代寻优
for i = 1 : maxgen
for j = 1 : sizepop
% 速度更新
V(j, :) = V(j, :) + c1 * rand * (gbest(j, :) - pop(j, :)) + c2 * rand * (zbest - pop(j, :));
V(j, (V(j, :) > Vmax)) = Vmax;
V(j, (V(j, :) < Vmin)) = Vmin;
% 种群更新
pop(j, :) = pop(j, :) + 0.2 * V(j, :);
pop(j, (pop(j, :) > popmax)) = popmax;
pop(j, (pop(j, :) < popmin)) = popmin;
% 适应度值
fitness(j) = fun(pop(j, :), hiddennum, net, p_train, t_train);
end
for j = 1 : sizepop
% 个体最优更新
if fitness(j) < fitnessgbest(j)
gbest(j, :) = pop(j, :);
fitnessgbest(j) = fitness(j);
end
end
BestFit = [BestFit, fitnesszbest];
end
%% 提取最优初始权值和阈值
w1 = zbest(1 : inputnum * hiddennum);
B1 = zbest(inputnum * hiddennum + 1 : inputnum * hiddennum + hiddennum);
w2 = zbest(inputnum * hiddennum + hiddennum + 1 : inputnum * hiddennum ...
+ hiddennum + hiddennum * outputnum);
B2 = zbest(inputnum * hiddennum + hiddennum + hiddennum * outputnum + 1 : ...
inputnum * hiddennum + hiddennum + hiddennum * outputnum + outputnum);
往期精彩
MATLAB实现RBF径向基神经网络多输入多输出预测
MATLAB实现BP神经网络多输入多输出预测
MATLAB实现DNN神经网络多输入多输出预测
参考资料
[1] https://blog.csdn.net/kjm13182345320/article/details/116377961
[2] https://blog.csdn.net/kjm13182345320/article/details/127931217
[3] https://blog.csdn.net/kjm13182345320/article/details/127894261