详解pytorch中循环神经网络(RNN、LSTM、GRU)的维度

详解pytorch中循环神经网络(RNN、LSTM、GRU)的维度

首先如果你对RNN、LSTM、GRU不太熟悉,可点击查看。

RNN

torch.nn.rnn详解

torch.nn.RNN(input_size,

hidden_size,

num_layers=1,

nonlinearity='tanh',

bias=True,

batch_first=False,

dropout=0.0,

bidirectional=False,

device=None,

dtype=None)

原理 :

参数详解:

  • input_size -- 输入x中预期特征的数量

  • hidden_size -- 隐藏状态h中的特征数量

  • num_layers -- 循环层数。例如,设置num_layers=2 意味着将两个LSTM堆叠在一起形成堆叠 LSTM,第二个 LSTM 接收第一个 LSTM 的输出并计算最终结果。默认值:1

  • nonlinearity-- 使用的非线性。可以是'tanh'或'relu'。默认:'tanh'

  • bias-- 如果False,则该层不使用偏差权重b_ih和b_hh。默认:True

  • batch_first -- 如果,则输入和输出张量以(batch, seq, feature)True形式提供,而不是(seq, batch, feature)。请注意,这不适用于隐藏状态或单元状态。默认:False

  • dropout -- 如果非零,则在除最后一层之外的每个LSTM层的输出上 引入Dropout层,dropout 概率等于 。默认值:0.0

  • bidirectional -- 如果True, 则成为双向LSTM。默认:False

RNN输入输出维度

python 复制代码
rnn = nn.RNN(10, 20, 2)
input = torch.randn(5, 3, 10)
h0 = torch.randn(2, 3, 20)
output, hn = rnn(input, h0)

可以看到输入是x和h_0,h_0可以是None。如果batch_size是第0维度,需设置batch_first=True。

输出则是output和h_n。h_n存了每一层的t时刻的隐藏状态值

python 复制代码
# Efficient implementation equivalent to the following with bidirectional=False
def forward(x, h_0=None):
    if batch_first:
        x = x.transpose(0, 1)
    seq_len, batch_size, _ = x.size()
    if h_0 is None:
        h_0 = torch.zeros(num_layers, batch_size, hidden_size)
	...
    return output, h_n

输入:
x的输入维度:(batch_size, sequence_length, input_size) 前提:batch_first=True
h_0的维度:(D∗num_layers, hidden_size) 可以为None
输出: output的输出维度:(batch_size, sequence_length, D*hidden_size)
D=2 if bidirectional=True otherwise 1
h_n的维度:(D∗num_layers, hidden_size)

LSTM

torch.nn.LSTM详解

torch.nn.LSTM(input_size,

hidden_size,

num_layers=1,

bias=True,

batch_first=False,

dropout=0.0,

bidirectional=False,

proj_size=0,

device=None,

dtype=None)

原理:

参数详解:

相比于RNN多了proj_size参数,少了nonlinearity参数

  • input_size -- 输入x中预期特征的数量

  • hidden_size -- 隐藏状态h中的特征数量

  • num_layers -- 循环层数。例如,设置num_layers=2 意味着将两个LSTM堆叠在一起形成堆叠 LSTM,第二个 LSTM 接收第一个 LSTM 的输出并计算最终结果。默认值:1

  • bias-- 如果False,则该层不使用偏差权重b_ih和b_hh。默认:True

  • batch_first -- 如果,则输入和输出张量以(batch, seq, feature)True形式提供,而不是(seq, batch, feature)。请注意,这不适用于隐藏状态或单元状态。默认:False

  • dropout -- 如果非零,则在除最后一层之外的每个LSTM层的输出上 引入Dropout层,dropout 概率等于 。默认值:0dropout

  • bidirectional -- 如果True, 则成为双向LSTM。默认:False

  • proj_size -- 如果,将使用具有相应大小投影的LSTM 。默认值:0

LSTM输入输出维度

python 复制代码
LSTM= nn.LSTM(10, 20, 2)
input = torch.randn(5, 3, 10)
h0 = torch.randn(2, 3, 20)
c0 = torch.randn(2, 3, 20)
output, (hn, cn) = LSTM(input, (h0, c0))

输入是x,此外h_0和c_0可以是None。如果batch_size是第0维度,需设置batch_first=True。

输出则是output和一个元组(h_n, c_n)。

输入: x的输入维度:(batch_size, sequence_length, input_size)`
前提:batch_first=True
输出: output的输出维度:(batch_size, sequence_length, D*hidden_size)
D=2 if bidirectional=True otherwise 1

具体可参考官方文档:nn.LSTM

GRU

torch.nn.GRU详解

torch.nn.GRU(input_size,

hidden_size,

num_layers=1,

bias=True,

batch_first=False,

dropout=0.0,

bidirectional=False,

device=None,

dtype=None)

原理:

参数详解:

与上文LSTM相比,缺少了proj_size参数,与RNN相比也缺少了nonlinearity参数

GRU输入输出维度

python 复制代码
gru= nn.GRU(10, 20, 2)
input = torch.randn(5, 3, 10)
h0 = torch.randn(2, 3, 20)
output, hn = gru(input, h0)

与RNN一致见上文,相比LSTM少了c_n

三种RNN的示例

python 复制代码
import torch
import torch.nn as nn

rnn = nn.RNN(10, 20, 2, batch_first=True) # (input_size, hidden_size, num_layer)
lstm = nn.LSTM(10, 20, 2, batch_first=True)
gru = nn.GRU(10, 20, 2, batch_first=True)

input = torch.randn(5, 3, 10)  # (batchsize, seq, input_size)
h0 = torch.randn(2, 3, 20)
c0 = torch.randn(2, 3, 20)

output_rnn, h_n = rnn(input)
output_lstm, (hn, cn) = lstm(input)
output_gru, h_n2 = gru(input)
print("输入维度:", input.shape)
print(f"RNN 输出维度:{output_rnn.shape}, h_n维度:{h_n.shape}" )
print("LSTM 输出维度:", output_lstm.shape)
print("GRU 输出维度:", output_gru.shape)


"""
输入维度: torch.Size([5, 3, 10])
RNN 输出维度:torch.Size([5, 3, 20]), h_n维度:torch.Size([2, 5, 20])
LSTM 输出维度: torch.Size([5, 3, 20])
GRU 输出维度: torch.Size([5, 3, 20])
"""
相关推荐
海天一色y4 小时前
模型部署的「通用语」:ONNX 转换
pytorch·onnx
Thomas.Sir10 小时前
第26课:工业零部件外观缺陷检测系统:从学术Demo到产线工程的重构实战
pytorch·ai
程序猿阿森11 小时前
Transformer 相对 CNN/RNN 的核心优势:全局依赖、并行计算与可扩展性
rnn·cnn·transformer
Zguigo1 天前
【CUDA6】CUDA Stream 是什么,为什么 CUDA 是异步执行,如何正确测量 GPU 时间以及多个任务如何重叠执行
人工智能·pytorch·深度学习
论文复现现场1 天前
RTX 3090 24GB 适合科研吗?单卡论文复现、PyTorch 显存检查与 OOM 排错
人工智能·pytorch·python·深度学习·cuda·rtx3090
论文复现现场2 天前
AutoDL、算家云与公有云 GPU 怎么选?环境复现、计费与断点恢复对比
pytorch·深度学习·云计算·gpu
盘古开天16662 天前
PPO算法代码实战(三):PyTorch从零实现PPO求解CartPole
人工智能·pytorch·算法
Light Gao2 天前
LSTM 与 GRU 详解:从门控机制到架构图与数值计算
人工智能·gru·lstm
Είναι η κοπέλα2 天前
PyTorch 安装与验证
人工智能·pytorch·python
盼小辉丶3 天前
PyTorch强化学习实战(27)——进化策略在强化学习中的应用
人工智能·pytorch·深度学习·强化学习