【代码实践】BiLSTM【待完善】

1、算法思路

Step 1: Calculate the daily cumulative sentiment score using the extracted news headlines related to crude oil price.

Step 2: Decompose the target variable (i.e., the daily logarithmic returns and 7-day volatility) into different intrinsic modes using VMD.

Step 3: Construct the forecasting input dataset by combining the daily cumulative sentiment score and the decomposed intrinsic modes.

Step 4: Organize the input dataset into a sliding window group with length of 7 days from time t 6 to time t.

Step 5: Import the input dataset into the BiLSTM neural networks to predict the daily returns and 7-day volatility at time t + 1.

Our BiLSTM prediction model consists of five layers: an input layer, a forward hidden layer, a backward hidden layer, an output layer and a fully-connected layer. The dimensions of the input layers, hidden layers and the output layer are all set to the same dimension as the input dataset. The fully-connected layer consists of one node that corresponds to the dimension of the final prediction value.

注意:这里的全连接层只含一个单元,即只输出一个预测值。因此,收益率和波动率是分开进行预测的。

The input dataset is first imported into the BiLSTM model through the first four layers, then it passes through the fully-connected layer to generate the daily logarithmic returns and 7-day volatility predictions at time t + 1. In this study, the input window length is set to 7 days, which is recommended as the optimal input length for short-term forecasting in previous literature (Zhu et al., 2019). The model utilizes the Adam optimizer with the learning rate set to 0.01. Within the LSTM cells, the tanh function is selected as the activation function.

 2、复现BiLSTM

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较常见的模型结构。以下是实现代码: 首先是导入需要的包: ```python import torch import torch.nn as nn import torch.nn.functional as F ``` 接下来是模型的主体结构: ```python class BiLSTM_SelfAttention(nn.Module): def __init__(self, num_tokens, embedding_size, hidden_size, num_layers): super(BiLSTM_SelfAttention, self).__init__() self.embedding = nn.Embedding(num_tokens, embedding_size) self.lstm = nn.LSTM(embedding_size, hidden_size, num_layers=num_layers, batch_first=True, bidirectional=True) self.self_attn = nn.MultiheadAttention(embed_dim=hidden_size * 2, num_heads=8) self.fc = nn.Linear(hidden_size * 2, 1) def forward(self, x): embedded = self.embedding(x) bilstm_output, _ = self.lstm(embedded) attn_output, _ = self.self_attn(bilstm_output.transpose(0, 1), bilstm_output.transpose(0, 1), bilstm_output.transpose(0, 1)) fc_output = self.fc(attn_output.squeeze(0)) return fc_output ``` 代码中,模型使用了一个 Embedding 层将输入的 tokens 转成 embedding ,使用了一个 BiLSTM 层将句子做一个 Bidirectional 的处理,接下来是 self-attention 进行得分计算,最后通过一个线性层转换为预测得分输出。注意 self-attention 层的输入需要将 BiLSTM 输出进行 transpose 处理,使得每个时刻的 hidden state 形状为 batch_size * hidden_size * num_directions。做完 self-attention 后再将表示转置回来即可。 这样就完成了一个 pytorch 实现的 bilstm-self-attention 模型。希望我的回答对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值