Pytorch LSTM 代码解读及自定义双向 LSTM 算子

本文深入解析Pytorch LSTM的理论基础,包括相关论文和理解材料,并详细解读官方源代码。此外,文章还介绍了如何从零开始自定义实现双向LSTM,通过测试证明了自定义实现与官方库的效果一致。
摘要由CSDN通过智能技术生成

Pytorch LSTM 代码解读及自定义双向 LSTM 算子

1. 理论

关于 LSTM 的理论部分可以参考

Paper
解析
Pytorch LSTM 算子

LSTMCell 前向计算过程如下:
在这里插入图片描述

2. 源代码

python 代码中仅仅能看到 _VF.lstm

# https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/rnn.py
# line 688
if batch_sizes is None:
	result = _VF.lstm(input, hx, self._flat_weights, self.bias, self.num_layers, 
						self.dropout, self.training, self.bidirectional, self.batch_first)
else:
	result = _VF.lstm(input, batch_sizes, hx, self._flat_weights, self.bias, self.num_layers,
    					self.dropout, self.training, self.bidirectional)

转到 C++ 代。代码逻辑比较清晰,最终的计算是在 LSTMCell 中实现的。

# https://github.com/pytorch/pytorch/blob/49777e67303f608987ec0948c7fd8f46f6d3ca83/torch/csrc/api/src/nn/modules/rnn.cpp
# line 275
std::tie(output, hidden_state, cell_state) = torch::lstm(
      input,
      {state[0], state[1]},
      flat_weights_,
      options.with_bias(),
      options.layers(),
      options.dropout(),
      this->is_training(),
      options.bidirectional(),
      options.batch_first());
# https://github.com/pytorch/pytorch/blob/1a93b96815b5c87c92e060a6dca51be93d712d09/aten/src/ATen/native/RNN.cpp
# line 855
std::tuple<Tensor, Tensor, Tensor> lstm(
      const Tensor& _input, TensorList hx,
      TensorList _params, bool has_biases,
      int64_t num_layers, double dropout_p, bool train, bool bidirectional, bool batch_first) {
  TORCH_CHECK(hx.size() == 2, "lstm expects two hidden states");
  if (at::cudnn_is_acceptable(_input)) {
    Tensor output, hy, cy;
    lstm_cudnn_stub(_input.type().device_type(), output, hy, cy, _input, hx, _params, has_biases,
            num_layers, dropout_p, train, bidirectional, batch_first);
    return std::make_tuple(output, hy, cy);
  } 

  if (use_miopen(_input, dropout_p)) {
    Tensor output, hy, cy;
    lstm_miopen_stub(_input.type()
下面是使用PyTorch实现双向LSTM代码示例: ```python import torch import torch.nn as nn # 定义双向LSTM模型 class BiLSTM(nn.Module): def __init__(self, input_size, hidden_size, num_layers, num_classes): super(BiLSTM, self).__init__() self.hidden_size = hidden_size self.num_layers = num_layers self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, bidirectional=True) self.fc = nn.Linear(hidden_size * 2, num_classes) # *2 是因为双向LSTM def forward(self, x): h0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size).to(x.device) # 初始隐藏状态,*2 是因为双向LSTM c0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size).to(x.device) # 初始细胞状态,*2 是因为双向LSTM out, _ = self.lstm(x, (h0, c0)) # 输出形状:(batch_size, seq_length, hidden_size*2) out = self.fc(out[:, -1, :]) # 取最后一个时间步的输出作为分类结果 return out # 定义模型参数 input_size = 10 # 输入特征维度 hidden_size = 20 # LSTM隐藏层维度 num_layers = 2 # LSTM层数 num_classes = 2 # 分类类别数 # 随机生成输入数据 batch_size = 3 seq_length = 5 x = torch.randn(batch_size, seq_length, input_size) # 创建模型实例并进行前向计算 model = BiLSTM(input_size, hidden_size, num_layers, num_classes) outputs = model(x) print(outputs.shape) # 输出形状:(batch_size, num_classes) ``` 上述代码定义了一个名为BiLSTM双向LSTM模型,包含一个双向LSTM层和一个全连接层。在前向计算中,我们首先初始化LSTM的隐藏状态和细胞状态,然后将输入数据传入双向LSTM层,并获取最后一个时间步的输出。最后,将该输出通过全连接层进行分类预测。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值