Pytorch中LSTM与GRU的使用与参数理解

Pytorch中LSTM与GRU的使用

在pytorch中,LSTM模块调用和GRU类似。下面调用以GRU为例。

GRU

初始化
rnn = nn.GRU(input_size, hidden_size, num_layers, bias, batch_first, dropout, bidirectional)
  • input_size: input的特征维度
  • hidden_size: 隐藏层的宽度
  • num_layers: 单元的数量(层数),默认为1,如果为2以为着将两个GRU堆叠在一起,当成一个GRU单元使用。
  • bias: True or False,是否使用bias项,默认使用
  • batch_first: Ture or False, 默认的输入是三个维度的,即:(seq, batch, feature),第一个维度是时间序列,第二个维度是batch,第三个维度是特征。如果设置为True,则(batch, seq, feature)。即batch,时间序列,每个时间点特征。
  • dropout:设置隐藏层是否启用dropout,默认为0
  • bidirectional:True or False, 默认为False,是否使用双向的GRU,如果使用双向的GRU,则自动将序列正序和反序各输入一次。

调用输入:

output = rnn(input, h_0)
  • input of shape (seq_len, batch, input_size): tensor containing the features
    of the input sequence. The input can also be a packed variable length
    sequence. See :func:torch.nn.utils.rnn.pack_padded_sequence
    for details.

    输入是3个维度,分别是时间步,batch,和特征。

    关于rnn.pack_padded_sequence [Pytorch中的RNN之pack_padded_sequence()和pad_packed_sequence()]

  • h_0 of shape (num_layers * num_directions, batch, hidden_size): tensor
    containing the initial hidden state for each element in the batch.
    Defaults to zero if not provided. If the RNN is bidirectional,
    num_directions should be 2, else it should be 1.

    隐藏层的初始化值。shape是(GRU单元的隐藏层数量x方向[1 or 2], batch, 隐藏层宽度)

输出:

  • output of shape (seq_len, batch, num_directions * hidden_size): tensor containing the output features h_t from the last layer of the GRU, for each t. If a class:torch.nn.utils.rnn.PackedSequence has been given as the input, the output will also be a packed sequence.
    For the unpacked case, the directions can be separated using output.view(seq_len, batch, num_directions, hidden_size), with forward and backward being direction 0 and 1 respectively. Similarly, the directions can be separated in the packed case.

    output形状:(序列长度,batch,方向[1 or 2]*隐藏层宽度) 。这个ouput包含了每个时间步的输出。可以使用output.view(seq_len, batch, num_directions, hidden_size)分解维度。

  • h_n of shape (num_layers * num_directions, batch, hidden_size): tensor
    containing the hidden state for t = seq_len Like output, the layers can be separated using
    h_n.view(num_layers, num_directions, batch, hidden_size).

    隐藏层形状:(GRU堆叠层数*方向[1 or 2], batch, 隐藏层的宽度)。可以使用h_n.view(num_layers, num_directions, batch, hidden_size)分解维度。

Examples1(官方源码)::

rnn = nn.GRU(10, 20, 2) # 输入的特征数10,隐藏层宽度20,2个GRU堆叠作为一个单元(2层)
input = torch.randn(5, 3, 10) # 随机初始化输入,
# 5个时间步,[5个序列(时间步)[batch=3[10个特征]]]
h0 = torch.randn(2, 3, 20) # 初始化隐藏层输入。
# 一次输入了5个时间步,3个batch
output, hn = rnn(input, h0) # 调用(input, 隐藏层)
    
Examples2()::当初始化条件:输入为1个时间步,此时GRU只有一个节点,在处理序列为题时,需要自己手写循环的数量。该层只相当于一个GRU单元。
rnn = nn.GRU(10,20,1) # input特征数为10,隐藏层宽度20, 1个GRU
input = torch.randn(1,1,10) # 随机输入,1个时间步,batch_size = 1, 特征数10
h0 = torch.randn(1,1,20)
output, hn = rnn(input, h0)

说明:当num_layers = 1时,output和hidden是相同的。当num_layers!=1时,就不相同了。如下图(图片来源)num_layers=w
在这里插入图片描述

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用PyTorch实现LSTM-GRU模型,以下是一个简单的示例代码: ```python import torch import torch.nn as nn # 定义LSTM-GRU模型 class LSTM_GRU(nn.Module): def __init__(self, input_size, hidden_size, num_layers, num_classes): super(LSTM_GRU, self).__init__() self.num_layers = num_layers self.hidden_size = hidden_size self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True) self.gru = nn.GRU(hidden_size, hidden_size, num_layers, batch_first=True) self.fc = nn.Linear(hidden_size, num_classes) def forward(self, x): # LSTM部分 h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device) c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device) out, _ = self.lstm(x, (h0, c0)) # GRU部分 out, _ = self.gru(out) # 输出部分 out = self.fc(out[:, -1, :]) return out ``` 在这个示例代码,我们定义了一个名为`LSTM_GRU`的类,它继承了`nn.Module`类,并实现了`__init__`和`forward`方法。 在`__init__`方法,我们定义了模型的各个,包括一个LSTM、一个GRU和一个全连接。`input_size`表示LSTMGRU的输入维度,`hidden_size`表示LSTMGRU隐藏维度,`num_layers`表示LSTMGRU数,`num_classes`表示模型输出的类别数。 在`forward`方法,我们首先通过LSTM对数据进行处理,然后将输出结果作为GRU的输入,再进行一次处理。最后通过全连接输出结果。 需要注意的是,这个示例代码的数据都是二维的,如果要处理更高维度的数据,需要对代码进行相应的修改。另外,还需要根据具体的任务对模型的各个参数进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值