CNN做时间序列预测_时间序列的LSTM预测

831307ddd2214a78c3b9438c676ebc35.png

背景

相关资料

(1)

Understanding LSTM Networks​colah.github.io
195952dc9982ebe246cc527c9bfd3c8a.png

(2)

pytorch示例​machinelearningmastery.com

LSTM解读

7de9df2acb6a90eba88432ddd6513c36.png
LSTM示意图

上图是LSTM相当经典的示意图了,上图中有三门一个细胞状态:

信息的前向传播满足:

其中

分别代表遗忘,输入,输出的比例系数,
分别代表候选状态,细胞态和隐藏层状态。比例系数都采用了sigmod函数限制了系数的范围,候选状态和输入以及上一个时间层的隐藏态的信息相关。细胞态可以认为是一种记忆单元,对记忆单元更新的时候,选择遗忘部分之前的记忆,接受部分新的信息,隐藏层的值直接从当前记忆细胞态获得信息,进过一个阀门输出(输出系数
控制)。

pytorch的LSTM继承自RNNBase,通常自己写的代码也需要继承LSTM模块,一个典型的例子为:

self

主要包含的参数为input_size,可以认为是sequence中一个word的维度,或者一个时间序列包含的特征数,hiddensize为隐藏层神经元个数,numlayers为网络在空间上的深度。bidirectional为True时,网络为双向的batch_first 为True时,输入的X的第一个维度是样本的batch_size,这对习惯了CNN的人来说会非常的亲切。虽然官方并不推荐设置batch_first为True,我看到很多Kaggle大佬都是这样写的[1],所以推荐大家也这样写。

68a058a0dc8463a3afa3d8baec35f56f.png
num_layers为3的LSTM网络

除了

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用CNN+BiLSTM+Attention进行多维时间序列预测可以提高预测的准确性。这种模型结构可以捕捉到时间序列的长期依赖关系和局部相关性,同时对不同维度的特征进行建模。下面是使用Keras实现CNN+BiLSTM+Attention的多维时间序列预测的示例代码。 ```python from keras.models import Model from keras.layers import Input, Conv1D, MaxPooling1D, Flatten, Dense, LSTM, Bidirectional, Dropout, concatenate, Permute, Reshape, Multiply from keras.optimizers import Adam from keras.callbacks import EarlyStopping, ReduceLROnPlateau from keras import backend as K def attention_3d_block(inputs): a = Permute((2, 1))(inputs) a = Dense(TIME_STEPS, activation='softmax')(a) a_probs = Permute((2, 1))(a) output_attention_mul = Multiply()([inputs, a_probs]) return output_attention_mul def build_model(): main_inputs = Input(shape=(TIME_STEPS, INPUT_DIM, )) cnn1 = Conv1D(filters=64, kernel_size=3, activation='relu', padding='same')(main_inputs) cnn1 = MaxPooling1D(pool_size=2)(cnn1) cnn1 = Dropout(0.2)(cnn1) cnn2 = Conv1D(filters=128, kernel_size=3, activation='relu', padding='same')(cnn1) cnn2 = MaxPooling1D(pool_size=2)(cnn2) cnn2 = Dropout(0.2)(cnn2) cnn3 = Conv1D(filters=256, kernel_size=3, activation='relu', padding='same')(cnn2) cnn3 = MaxPooling1D(pool_size=2)(cnn3) cnn3 = Dropout(0.2)(cnn3) lstm = Bidirectional(LSTM(units=128, return_sequences=True, dropout=0.5))(cnn3) attention_mul = attention_3d_block(lstm) attention_mul = Flatten()(attention_mul) dense1 = Dense(units=64, activation='relu')(attention_mul) dense1 = Dropout(0.5)(dense1) dense2 = Dense(units=1, activation='linear')(dense1) model = Model(inputs=main_inputs, outputs=dense2) model.compile(optimizer=Adam(lr=0.001), loss='mse') return model ``` 在这个模型,我们使用了三个卷积层、一个双向LSTM层和一个Attention层。卷积层用于提取时间序列的局部特征,LSTM层用于捕捉时间序列的长期依赖关系,Attention层用于加强对LSTM的关注。 我们使用了EarlyStopping和ReduceLROnPlateau两个回调函数来帮助我们控制模型的训练过程。其EarlyStopping用于防止过拟合,ReduceLROnPlateau用于调整学习率。 ```python # 训练模型 model = build_model() early_stopping = EarlyStopping(monitor='val_loss', patience=10) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5) history = model.fit(X_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS, validation_data=(X_val, y_val), callbacks=[early_stopping, reduce_lr]) # 预测结果 y_pred = model.predict(X_test) # 评估模型 score = model.evaluate(X_test, y_test, batch_size=BATCH_SIZE) print('Test loss:', score) ``` 在训练过程,我们可以通过调用fit函数来训练模型。在预测时,我们可以使用predict函数来进行预测。在评估模型时,我们可以使用evaluate函数来计算模型的损失。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值