【LSTM时序预测】基于灰狼算法优化双向长短时记忆GWO-biLSTM时序股价预测(含前后对比)附Matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

更多Matlab仿真内容点击👇

智能优化算法       神经网络预测       雷达通信       无线传感器        电力系统

信号处理              图像处理               路径规划       元胞自动机        无人机 

⛄ 内容介绍

一种基于灰狼算法优化LSTM的网络流量预测方法,属于网络流量预测领域,该方法包括以下步骤:对第一网络流量数据集进行极差标准化处理,得到第二网络流量数据集,并划分为训练集和测试集,并确定灰狼算法优化LSTM神经网络中输入层单元个数,输出层单元个数和隐藏层单元个数;用得到第二网络流量数据集,在过程中利用灰狼算法来优化LSTM神经网络的参数,得到训练完成的决策算法优化LSTM神经网络;灰狼优化算法可以有效解决优化中的求解问题,可应用于工程,经济,调度等问题求解.灰狼优化算法利用自身的全局最优能力弥补传统LSTM容易收敛于局部最优解的缺点,提高LSTM避免局部最优能力;加快LSTM神经网络参数收速度.实验证明,灰狼算法优化LSTM能够有效减少神经网络的训练时间,提高了股价预测精度.

LSTM 是循环神经网络中的一个特殊网络,它能够很好的处理序列信息并从中学习有效特征,它把以往的神经单元用一个记忆单元( memory cell) 来代替,解决了以往循环神经网络在梯度反向传播中遇到的爆炸和衰减问题. 一个记忆单元利用了输入门 it、一个记忆细胞 ct、一个忘记门 ft、一个输出门 ot 来控制历史信息的储存记忆,在每次输入后会有一个当前状态 ht,ht 计算如下:

其中,xt 为 t 时刻输入的情感词向量,σ 为 sigmoid 函数, 代表向量对应元素依次相乘,其中水电费 Wi,Ui,Vi,bi, Wg,Ug,bg,Wo,Uo,Vo,bo 为 LSTM 参数.

⛄ 部分代码

clc

clear all

close all hidden

disp('****************************ANTENNA DIPOLE PROJECT******************************')

disp('                             farshid.azhir                                  ')

disp('NOTICE!:This project includes 3type of dipole 1)INFINITESIMALE DIPOLE') 

disp('                                              2)SMALL DIPOLE')

disp('                                              3)FINIT LENGHT DIPOLE')

                                                   

F=input('Please Enter Frequency F(Hz)=\n');

disp('----------------')

lambda=(3e8/F)

disp('----------------')

L=input('Please Enter length L(meter)=\n');

disp('----------------')

I=input('Please Enter Current(Amplitude) I0=');

T=input('Please Enter current(Phase) theta=');

i=I.*exp(j.*T)

disp('              ')

w=2.*pi.*F;

B=2*pi/lambda;

etha=377;

if L<=lambda/50

    disp('~~~~~~~~~~~~~~~~~~~~~~~INFINITESIMALE DIPOLE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')

r=1000*lambda/(2*pi);

x=0:0.0005:pi;

q=0:0.001:2*pi;

E=(1./(4.*pi.*r)).*etha.*B.*I.*L.*sin(x).*cos((2.*pi.*F).*q-B.*r+pi./2+T);

subplot(2,2,1)

plot(x,E)

title('Electric Field')

H=(1./(4.*pi.*r)).*B.*I.*L.*sin(x).*cos((2.*pi.*F).*q-B.*r+pi./2+T);

subplot(2,2,2)

plot(x,H)

title('Magnetic Field')

subplot(2,2,3)

P=sin(x);

polar(x,P)

hold on

p=sin(-x);

polar(x,p)

view(-270,-90)

title('Antenna pattern')

disp('**************************  Resistance density(Rr)=   ***************************')

Rr=80.*(pi).*(pi).*(L/lambda)^2

disp('**************************  Directivity=  ****************************************')

D=3/2

%current plot

subplot(2,2,4)

z=-L/2:0.001:L/2;

ii=I.*cos(2.*pi.*F.*z+T);

plot(ii,z)

title('Current distribution')

figure

% 1.- 3-D Mesh: Azimut & Elevation 

%---------------------------------- 

n_tehta = 130; % Samples on Elevation 

n_phi = 130; % Samples on Azimut 

[tehta,phi]=meshgrid(eps:pi./(n_tehta-1):pi,... 

            0:2*pi./(n_phi-1):2*pi) ; 

radio = sin(tehta); 

X=radio.*sin(tehta).*cos(phi); 

Y=radio.*sin(tehta).*sin(phi); 

Z=radio.*cos(tehta); 

surf(X,Y,Z)

camlight right

light

shading interp

colorbar

axis image 

rotate3D on

TITLE('3D-Pattern plot')

elseif (L>lambda/50)&(L<=lambda/10)

    %SMALL DIPOLE

    %CALCULATE-------------------------------------------------------

  disp('~~~~~~~~~~~~~~~~~~SMALL DIPOLE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')

r=1000*lambda/(2*pi);

x=0:0.0005:pi;

q=0:0.001:2*pi;

E=(1./(8.*pi.*r)).*etha.*B.*I.*L.*sin(x).*cos(((w).*q)-B.*r+pi./2+T);

subplot(2,2,1)

plot(x,E)

title('Electric Field')

H=(1./(8.*pi.*r)).*B.*I.*L.*sin(x).*cos(((w).*q)-B.*r+pi./2+T);

subplot(2,2,2)

plot(x,H)

title('Magnetic Field')

subplot(2,2,3)

P=sin(x);

polar(x,P)

hold on

p=sin(-x);

polar(x,p)

view(-270,-90)

title('Antenna pattern')

disp('**************************  Resistance density(Rr)=   ***************************')

Rr=20.*(pi).*(pi).*(L/lambda).^2

disp('**************************  Directivity=  ****************************************')

D=3/2

subplot(2,2,4)

z=0:0.000001:L/2;

ii=I.*cos(2.*pi.*F.*z+T).*(1-(2/L).*z);

plot(ii,z)

hold on

z=-L/2:0.000001:0;

ii=I.*cos(2.*pi.*F.*z+T).*(1+(2/L).*z);

plot(ii,z)

figure

% 1.- 3-D Mesh: Azimut & Elevation 

n_tehta = 130; % Samples on Elevation 

n_phi = 130; % Samples on Azimut 

[tehta,phi]=meshgrid(eps:pi./(n_tehta-1):pi,... 

            0:2*pi./(n_phi-1):2*pi) ; 

radio = sin(tehta); 

X=radio.*sin(tehta).*cos(phi); 

Y=radio.*sin(tehta).*sin(phi); 

Z=radio.*cos(tehta); 

surf(X,Y,Z)

camlight right

light

shading interp

colorbar

axis image 

rotate3D on

TITLE('3D-Pattern plot')

else

    disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~FINITE LENGTH DIPOLE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')

       r=1000000*lambda/(2*pi);

x=eps:0.001:2*pi;

q=eps:0.001:2*pi;

A=cos(B.*L/2);

p=cos(cos(x).*B.*L/2)-A;

m=p./sin(x);

E=(1./(2.*pi.*r)).*etha.*B.*I.*L.*m.*cos(((w).*q)-B.*r+pi./2+T);

subplot(2,3,1)

plot(x,E)

title('Electric Field')

H=(1./(2.*pi.*r)).*B.*I.*L.*m.*cos(((w).*q)-B.*r+pi./2+T);

subplot(2,3,2)

plot(x,H)

title('Magnetic Field')

subplot(2,3,3)

polar(x,m,'r')

hold on

m=-p./sin(x);

polar(x,m,'r')

view(-270,-90)

title('Antenna pattern')

subplot(2,3,4)

z=0:0.00001:L/2;

ii=I.*cos((w).*z+T).*(sin(B.*(-z+L/2)));

plot(ii,z)

hold on

z=-L/2:0.00001:0;

ii=I.*cos((w).*z+T).*(sin(B.*(z+L/2)));

plot(ii,z)

grid on

title('Current Distribution')

disp('**************************  Resistance density(Rr)=   ***************************')

rr=(0.5772+log(B*L))-cosint(B*L);

ro=rr+(1/2).*sin(B*L)*(sinint(2*B*L)-2*sinint(B*L));

Q=ro+(1/2).*cos(B*L)*(0.5772+log(B*L/2)+cosint(2*B*L)-2*cosint(B*L));

Rr=(etha/(2*pi)).*Q

subplot(2,3,5)

D=2.*(m.^2)./Q;

polar(x,D,'k')

view(-270,-90)

title('Directivity')

figure

% 1.- 3-D Mesh: Azimut & Elevation 

n_tehta = 130; % Samples on Elevation 

n_phi = 130; % Samples on Azimut 

[tehta,phi]=meshgrid(eps:pi./(n_tehta-1):pi,... 

            0:2*pi./(n_phi-1):2*pi) ; 

Bas = L/lambda; % Half Wave Dipole 

Num = cos(pi*Bas*cos(tehta))-cos(pi*Bas); 

Den = sin(tehta); 

radio = Num./Den; 

X=radio.*sin(tehta).*cos(phi); 

Y=radio.*sin(tehta).*sin(phi); 

Z=radio.*cos(tehta); 

surf(X,Y,Z)

camlight right

light

shading interp

colorbar

axis image 

rotate3D on

TITLE('3D-Pattern plot')

end

disp('          ')

disp('press any key to go to manipulation of feeder section')

pause

%-------------------manipulation of feeder--------

clc

close all hidden

disp('....................................change posision of feeder...........................')

f=input('Enter Frequency:\n'); 

 disp('------------------------------------------------------------')

  lambda=3e8/f

 disp('------------------------------------------------------------')

  L=input('Enter L(length of dipole:\n');

 disp('Please enter any key to continue...')

 pause

 disp('------------------------------------------------------------')

 disp('L/2')

 L/2

 disp('------------------------------------------------------------') 

 i=input('current:\n');

 b=2*pi/lambda;

  n=L/(lambda/2);

  if n<=1;

    disp('dipole antenna is lambda/2 or smaller ')

    disp('---------------------------------------')

    h=input('position of feeder,posision must be 0<position<L/2 or -L/2<position<0\n--->');

if (h>L/2)|(h<-L/2)

    error('you enter position feeder greater than Length of dipole')

    return

end

disp('              ')

disp('Rradiational when change posision feeder=')

Rr_new=Rr./((sin(B.*((L/2)-h))).^2) 

Rr_old=Rr

xp=h;

yp=-i:0.00001:-i+(i/100);

plot(yp,xp,'dr')

hold on

x=-L/2:0.01:L/2;

y=i*sin(b*((L/2)-(x+h)));

plot(y,x)

grid on

elseif n==2;

    

     disp('Length dipole antenna is lambda ')

    disp('---------------------------------------')

        h=input('position of feeder,posision must be 0<position<L/2 or -L/2<position<0\n--->');

if (h>L/2)|(h<-L/2)

    error('you enter position feeder greater than Length of dipole')

    return

end

disp('              ')

disp('Rradiational when change posision feeder=')

Rr_new=Rr./((sin(B.*((L/2)-h))).^2) 

Rr_old=Rr

xp=h;

yp=-i:0.001:-i+(i/100);

plot(yp,xp,'sr')

hold on

grid on

if h==0

  x=0:0.01:L/2;

y=i*sin(b*((L/2)-(x+h)));

plot(y,x)

hold on

x=-L/2:0.01:0;

y=i*sin(b*((L/2)+(x+h)));

plot(y,x)

grid on  

elseif h>0    

x=0:0.01:L/2;

y=i*sin(b*((L/2)-(x+h)));

plot(y,x)

hold on

x=-L/2:0.01:0;

y=i*sin(pi+b*((L/2)+(x+h)));

plot(y,x)

grid on

else

 x=-L/2:0.01:0;

y=i*sin(pi+b*((L/2)-(x+h)));

plot(y,x)

hold on

x=0:0.01:L/2;

y=i*sin(b*((L/2)+(x+h)));

plot(y,x)

grid on

end

elseif n==3;

       disp('Length dipole antenna is 3*lambda/2 ')

    disp('---------------------------------------')

        h=input('position of feeder,posision must be 0<position<L/2 or -L/2<position<0\n--->');

if (h>L/2)|(h<-L/2)

    error('you enter position feeder greater than Length of dipole')

    return

end 

disp('              ')

disp('Rradiational when change posision feeder=')

Rr_new=Rr./((sin(B.*((L/2)-h))).^2) 

Rr_old=Rr

xp=h;

yp=-i:0.001:-i+(i/100);

plot(yp,xp,'sr')

hold on

x=-L/2:0.01:L/2;

y=i*sin(pi+b*((L/2)-(x+h)));

plot(y,x)

grid on

elseif n==4;

      disp('Length dipole antenna is 2*lambda ')

    disp('---------------------------------------')

        h=input('position of feeder,posision must be 0<position<L/2 or -L/2<position<0\n--->');

if (h>L/2)|(h<-L/2)

    error('you enter position feeder greater than Length of dipole')

    return

end

disp('              ')

disp('Rradiational when change posision feeder=')

Rr_new=Rr./((sin(B.*((L/2)-h))).^2) 

Rr_old=Rr

xp=h;

yp=-i:0.001:-i+0.1;

plot(yp,xp,'sr')

hold on

grid on

if h==0

  x=0:0.01:L/2;

y=i*sin(b*((L/2)-(x+h)));

plot(y,x)

hold on

x=-L/2:0.01:0;

y=i*sin(b*((L/2)+(x+h)));

plot(y,x)

grid on  

elseif (0<h)&(h<lambda/2);

    x=0:0.01:lambda/2;

y=i*sin(pi+b*((L/2)-(x+h)));

plot(y,x)

hold on

x=lambda/2:0.01:lambda;

y=i*sin(pi+b*((L/2)-(x+h)));

plot(y,x)

x=0:-0.01:-lambda/2;

y=i*sin(pi+b*((L/2)-(x+h)));

plot(y,x)

x=-lambda/2:-0.01:-lambda;

y=i*sin(pi+b*((L/2)-(x+h)));

plot(y,x)

grid on

elseif ((lambda/2)<h)&(h<lambda);  

    x=lambda/2:0.01:lambda;

y=i*sin(pi+b*((L/2)-(x+h)));

plot(y,x)

hold on

x=0:0.01:lambda/2;

y=i*sin(pi+b*((L/2)-(x+h)));

plot(y,x)

x=0:-0.01:-lambda/2;

y=i*sin(pi+b*((L/2)-(x+h)));

plot(y,x)

x=-lambda/2:-0.01:-lambda;

y=i*sin(pi+b*((L/2)-(x+h)));

plot(y,x)

grid on

elseif ((-lambda/2)<h)&(h<0);  

x=lambda/2:0.01:lambda;

y=i*sin(pi+b*((L/2)+(x+h)));

plot(y,x)

hold on

x=0:0.01:lambda/2;

y=i*sin(pi+b*((L/2)+(x+h)));

plot(y,x)

x=0:-0.01:-lambda/2;

y=i*sin(pi+b*((L/2)+(x+h)));

plot(y,x)

x=-lambda/2:-0.01:-lambda;

y=i*sin(pi+b*((L/2)+(x+h)));

plot(y,x)

grid on

else

    ((-lambda/2)>h)&(h>(-lambda));  

x=lambda/2:0.01:lambda;

y=i*sin(pi+b*((L/2)+(x+h)));

plot(y,x)

hold on

x=0:0.01:lambda/2;

y=i*sin(pi+b*((L/2)+(x+h)));

plot(y,x)

x=0:-0.01:-lambda/2;

y=i*sin(pi+b*((L/2)+(x+h)));

plot(y,x)

x=-lambda/2:-0.01:-lambda;

y=i*sin(pi+b*((L/2)+(x+h)));

plot(y,x)

grid on

end

else 

disp('Length dipole antenna is greater than 2*lambda and undefined ')

    disp('--------------------Exit----------------------')

end

⛄ 运行结果

⛄ 参考文献

[1]杜秀丽等. "一种基于决策灰狼算法优化LSTM的网络流量预测方法.", CN111371607A. 2020.

⛄ 完整代码

❤️部分理论引用网络文献,若有侵权联系博主删除

❤️ 关注我领取海量matlab电子书和数学建模资料

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1.版本:matlab2014/2019a/2021a,内运行结果,不会运行可私信 2.领域:智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,更多内容可点击博主头像 3.内容:标题所示,对于介绍可点击主页搜索博客 4.适合人群:本科,硕士等教研学习使用 5.博客介绍:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可si信 %% 开发者:Matlab科研助手 %% 更多咨询关注天天Matlab微信公众号 ### 团队期从事下列领域算法的研究和改进: ### 1 智能优化算法及应用 **1.1 改进智能优化算法方面(单目标和多目标)** **1.2 生产调度方面** 1.2.1 装配线调度研究 1.2.2 车间调度研究 1.2.3 生产线平衡研究 1.2.4 水库梯度调度研究 **1.3 路径规划方面** 1.3.1 旅行商问题研究(TSP、TSPTW) 1.3.2 各类车辆路径规划问题研究(vrp、VRPTW、CVRP) 1.3.3 机器人路径规划问题研究 1.3.4 无人机三维路径规划问题研究 1.3.5 多式联运问题研究 1.3.6 无人机结合车辆路径配送 **1.4 三维装箱求解** **1.5 物流选址研究** 1.5.1 背包问题 1.5.2 物流选址 1.5.4 货位优化 ##### 1.6 电力系统优化研究 1.6.1 微电网优化 1.6.2 配电网系统优化 1.6.3 配电网重构 1.6.4 有序充电 1.6.5 储能双层优化调度 1.6.6 储能优化配置 ### 2 神经网络回归预测时序预测、分类清单 **2.1 bp预测和分类** **2.2 lssvm预测和分类** **2.3 svm预测和分类** **2.4 cnn预测和分类** ##### 2.5 ELM预测和分类 ##### 2.6 KELM预测和分类 **2.7 ELMAN预测和分类** ##### 2.8 LSTM预测和分类 **2.9 RBF预测和分类** ##### 2.10 DBN预测和分类 ##### 2.11 FNN预测 ##### 2.12 DELM预测和分类 ##### 2.13 BIlstm预测和分类 ##### 2.14 宽度学习预测和分类 ##### 2.15 模糊小波神经网络预测和分类 ##### 2.16 GRU预测和分类 ### 3 图像处理算法 **3.1 图像识别** 3.1.1 车牌、交通标志识别(新能源、国内外、复杂环境下车牌) 3.1.2 发票、身份证、银行卡识别 3.1.3 人脸类别和表情识别 3.1.4 打靶识别 3.1.5 字符识别(字母、数字、手写体、汉字、验证码) 3.1.6 病灶识别 3.1.7 花朵、药材、水果蔬菜识别 3.1.8 指纹、手势、虹膜识别 3.1.9 路面状态和裂缝识别 3.1.10 行为识别 3.1.11 万用表和表盘识别 3.1.12 人民币识别 3.1.13 答题卡识别 **3.2 图像分割** **3.3 图像检测** 3.3.1 显著性检测 3.3.2 缺陷检测 3.3.3 疲劳检测 3.3.4 病害检测 3.3.5 火灾检测 3.3.6 行人检测 3.3.7 水果分级 **3.4 图像隐藏** **3.5 图像去噪** **3.6 图像融合** **3.7 图像配准** **3.8 图像增强** **3.9 图像压缩** ##### 3.10 图像重建 ### 4 信号处理算法 **4.1 信号识别** **4.2 信号检测** **4.3 信号嵌入和提取** **4.4 信号去噪** ##### 4.5 故障诊断 ##### 4.6 脑电信号 ##### 4.7 心电信号 ##### 4.8 肌电信号 ### 5 元胞自动机仿真 **5.1 模拟交通流** **5.2 模拟人群疏散** **5.3 模拟病毒扩散** **5.4 模拟晶体生** ### 6 无线传感器网络 ##### 6.1 无线传感器定位(Dv-Hop定位优化、RSSI定位优化) ##### 6.2 无线传感器覆盖优化 ##### 6.3 无线传感器通信及优化(Leach协议优化) ##### 6.4 无人机通信中继优化(组播优化
贝叶斯优化是一种结合贝叶斯统计和机器学习的方法,可以用于优化深度学习模型的超参数。BILSTM是一种双向短期记忆网络,可以用于时序数据的预测。在Matlab中,可以结合这两种方法进行时序预测代码编写。 首先,需要加载时序数据,可以使用Matlab中的csvread或者load函数。然后,构建BILSTM模型,可以使用Matlab中的LSTMLayer函数来搭建模型结构。接下来,定义模型的超参数,例如学习率、隐藏层单元数等,并使用贝叶斯优化方法来搜索最优的超参数组合。可以使用Matlab中的bayesopt函数来进行贝叶斯优化的参数搜索。 在贝叶斯优化的过程中,可以定义目标函数,即待优化的指标,例如均方根误差(RMSE)或者平均绝对误差(MAE)。然后,通过调用训练函数来训练BILSTM模型,并计算目标函数的数值。贝叶斯优化方法会不断调整超参数的取值,直到找到使目标函数最小化的超参数组合。 最后,可以使用训练好的BILSTM模型进行时序预测预测结果可以通过画图或者计算评价指标来进行评估。在Matlab中,可以使用plot函数来画出真实值和预测值的对比图,也可以使用rmse或者mae函数来计算预测误差指标。 综上所述,基于贝叶斯优化BILSTM的时序预测Matlab代码编写包括数据加载、模型构建、超参数设置、贝叶斯优化、目标函数定义、模型训练和预测结果评估等步骤。通过这些步骤,可以有效地优化BILSTM模型的超参数,并进行时序预测分析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matlab科研助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值