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

本文详细介绍了在PyTorch中如何使用LSTM和GRU进行序列建模,包括它们的参数设置、输入格式、初始化过程以及实例演示。重点讲解了这两种门控循环神经网络的区别与应用场景,适合初学者和进阶者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值