赞
踩
由于参加了一个小的课题,是关于时间序列预测的。平时习惯用matlab, 网上这种资源就比较少。
借鉴了 http://blog.csdn.net/u010540396/article/details/52797489 的内容,稍微修改了一下程序。
程序说明:DATA.mat 是一行时序值,
numdely 是用前numdely个点预测当前点,cell_num是隐含层的数目,cost_gate 是误差的阈值。
直接在命令行输入RunLstm(numdely,cell_num,cost_gate)即可。
function [r1, r2] = RunLstm(numdely,cell_num,cost_gate)
%% 数据加载,并归一化处理
figure;
[train_data,test_data]=LSTM_data_process(numdely);
data_length=size(train_data,1)-1;
data_num=size(train_data,2);
%% 网络参数初始化
% 结点数设置
input_num=data_length;
% cell_num=5;
output_num=1;
% 网络中门的偏置
bias_input_gate=rand(1,cell_num);
bias_forget_gate=rand(1,cell_num);
bias_output_gate=rand(1,cell_num);
%网络权重初始化
ab=20;
weight_input_x=rand(input_num,cell_num)/ab;
weight_input_h=rand(output_num,cell_num)/ab;
weight_inputgate_x=rand(input_num,cell_num)/ab;
weight_inputgate_c=rand(cell_num,cell_num)/ab;
weight_forgetgate_x=rand(input_num,cell_num)/ab;
weight_forgetgate_c=rand(cell_num,cell_num)/ab;
weight_outputgate_x=rand(input_num,cell_num)/ab;
weight_outputgate_c=rand(cell_num,cell_num)/ab;
%hidden_output权重
weight_preh_h=rand(cell_num,output_num);
%网络状态初始化
% cost_gate=0.25;
h_state=rand(output_num,data_num);
cell_state=rand(cell_num,data_num);
%% 网络训练学习
for iter=1:100
yita=0.01; %每次迭代权重调整比例
for m=1:data_num
%前馈部分
if(m==1)
gate=tanh(train_data(1:input_num,m)'*weight_input_x);
input_gate_input=train_data(1:input_num,m)'*weight_inputgate_x+bias_input_gate;
output_gate_input=train_data(1:input_num,m)'*weight_outputgate_x+bias_output_gate;
for n=1:cell_num
input_gate(1,n)=1/(1+exp(-input_gate_input(1,n)));
output_gate(1,n)=1/(1+exp(-output_gate_input(1,n)));
end
forget_gate=zeros(1,cell_num);
forget_gate_input=zeros(1,cell_num);
cell_state(:,m)=(input_gate.*gate)';
else
gate=tanh(train_data(1:input_num,m)'*weight_input_x+h_state(:,m-1)'*weight_input_h);
input_gate_input=train
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。