LSTM和GRU

GRU

引入

在循环神经网络RNN中,较长的时间步在反向传播中可能导致梯度消失或者梯度爆炸(梯度爆炸可以使用梯度裁剪,而梯度消失则是导致RNN性能瓶颈的主要原因),因此只能选用较小的时间步或者在长序列上使用截断时间步。传统RNN因为梯度消失的问题,在超过20步时记忆能力显著下降。

在一个长序列中,并不是每个时间步的信息都非常重要,因此,有些时间步上的信息可以不给予太多的关注,而有些时间步上的信息则很重要,如果模型能够自主决定记住哪些时间步上的信息,遗忘哪些时间步上的信息,那么模型就可以在更长的时间步上记住更重要的内容从而达到更好的性能。

  • 我们可能会遇到这样的情况:一些词元没有相关的观测值。 例如,在对网页内容进行情感分析时, 可能有一些辅助HTML代码与网页传达的情绪无关。 我们希望有一些机制来跳过隐状态表示中的此类词元。
  • 我们可能会遇到这样的情况:序列的各个部分之间存在逻辑中断。 例如,书的章节之间可能会有过渡存在, 或者证券的熊市和牛市之间可能会有过渡存在。 在这种情况下,最好有一种方法来重置我们的内部状态表示。

在学术界已经提出了许多方法来解决这类问题。其中最早的方法是“长短期记忆”(long-short-term memory, LSTM)。门控循环单元(gated recurrent unit, GRU)是一个稍微简化的变体,通常能够提供同等的效果,并且计算的速度明显变快。

模型

重置门和更新门

重置门和更新门

\(\mathbf{X}_t\in\mathbb{R}^{n\times d}\)(样本个数\(n\),输入个数\(d\))代表\(t\)时刻的输入数据,\(\mathbf{H}_t\in\mathbb{R}^{n\times h}\)(隐藏单元个数\(h\))代表\(t\)时刻的隐藏状态。那么,重置门(Reset gate)\(\mathbf{R}_t\in\mathbb{R}^{n\times h}\)和更新门(Update gate)\(\mathbf{Z}_t\in\mathbb{R}^{n\times h}\)的计算如下: \[ \begin{aligned} &\mathbf{R}_t = \sigma(\mathbf{X}_t\mathbf{W}_{xr} + \mathbf{H}_{t-1}\mathbf{W}_{hr} + \mathbf{b}_r)\\ &\mathbf{Z}_t = \sigma(\mathbf{X}_t\mathbf{W}_{xz} + \mathbf{H}_{t-1}\mathbf{W}_{hz} + \mathbf{b}_z) \end{aligned} \] 其中,\(\mathbf{W}_{xr}, \mathbf{W}_{xz}\in\mathbb{R}^{d\times h}\)\(\mathbf{W}_{hr}, \mathbf{W}_{hz}\in\mathbb{R}^{h\times h}\)是权重参数,\(\mathbf{b}_r, \mathbf{b}_z\in\mathbb{R}^{1\times h}\)是偏置参数(会触发广播机制)。\(\sigma\)是sigmoid函数,因此\(\mathbf{R}_t\)\(\mathbf{Z}_t\)中每个元素的值均在\((0, 1)\)之间

候选隐状态

\[ \mathbf{\widetilde H}_t = \tanh\Big(\mathbf{X}_t\mathbf{W}_{xh} + (\mathbf{R}_t\odot\mathbf{H}_{t-1})\mathbf{W}_{hh} + \mathbf{b}_h \Big) \] 其中,\(\mathbf{W}_{xh}\in\mathbb{R}^{d\times h}\)\(\mathbf{W}_{hh}\in\mathbb{R}^{h\times h}\)是权重参数,\(\mathbf{b}_h\in\mathbb{R}^{1\times h}\)是偏置项,符号\(\odot\)是哈达玛积(按元素乘积)运算符。

\(\mathbf{R}_t\odot\mathbf{H}_{t-1}\)可以认为是上一个时间步的隐藏状态\(\mathbf{H}_{t-1}\)还保留多少,在极端情况下:

  • \(\mathbf{R}_t\)中所有元素均为0时,\(\mathbf{\widetilde H}_t = \tanh(\mathbf{X}_t\mathbf{W}_{xh} + \mathbf{b}_h)\),也就是不保留之前的所有信息
  • \(\mathbf{R}_t\)中所有元素均为1时,\(\mathbf{\widetilde H}_t = \tanh(\mathbf{X}_t\mathbf{W}_{xh} + \mathbf{H}_{t-1}\mathbf{W}_{hh} + \mathbf{b}_h)\),也就等价于之前的RNN

需要注意的是,\(\mathbf{\widetilde H}_t\)\(t\)时刻的候选隐状态,并不是隐藏状态\(\mathbf{H}_t\)

在这里使用的是\(\tanh\)非线性激活函数来确保候选隐状态中的值保持在区间\((-1, 1)\)

由上述公式可见,重置门可以认为是保留多少之前的信息,即对信息的充值程度,当\(\mathbf{R}_t\)内的元素值越接近0,重置程度越大

隐状态

\[ \mathbf{H}_t = \mathbf{Z}_t\odot\mathbf{H}_{t-1} + (1 - \mathbf{Z}_t)\odot\mathbf{\widetilde H}_t \] 每当更新门\(\mathbf{Z}_t\)中元素接近1时,模型就倾向于只保留旧状态。此时,来自\(\mathbf{X}_t\)的信息基本上被忽略,从而有效地跳过了依赖链条中的时间步\(t\)。相反,当\(\mathbf{Z}_t\)中元素接近0时,新的隐状态\(\mathbf{H}_t\)就会接近候选隐状态\(\mathbf{\widetilde H}_t\)。这些设计可以帮我们处理循环神经网络中的梯度消失的问题,并更好地捕获时间步距离很长的序列依赖关系。例如,如果整个子序列的所有时间步的更新门都接近于1,则无论序列的长度如何,在序列起始时间步的旧隐状态都将很容易保留并传递到序列结束

总之,门控循环单元具有以下两个显著特征:

  • 重置门有助于捕获序列中的短期依赖关系
  • 更新门有助于捕获序列中的长期依赖关系

前向传播

\[ \begin{aligned} &\mathbf{R}_t = \sigma(\mathbf{X}_t\mathbf{W}_{xr} + \mathbf{H}_{t-1}\mathbf{W}_{hr} + \mathbf{b}_r)\\ &\mathbf{Z}_t = \sigma(\mathbf{X}_t\mathbf{W}_{xz} + \mathbf{H}_{t-1}\mathbf{W}_{hz} + \mathbf{b}_z)\\ &\mathbf{\widetilde H}_t = \tanh\Big(\mathbf{X}_t\mathbf{W}_{xh} + (\mathbf{R}_t\odot\mathbf{H}_{t-1})\mathbf{W}_{hh} + \mathbf{b}_h \Big)\\ &\mathbf{H}_t = \mathbf{Z}_t\odot\mathbf{H}_{t-1} + (1 - \mathbf{Z}_t)\odot\mathbf{\widetilde H}_t \end{aligned} \]

反向传播

对于\(\partial \mathbf{H}_{t}/\partial \mathbf{H}_{t-1}\)\[ \begin{aligned} \frac{\partial \mathbf{H}_t}{\partial \mathbf{H}_{t-1}} &= \frac{\partial (\mathbf{Z}_t\odot\mathbf{H}_{t-1}+(1-\mathbf{Z}_t)\odot\mathbf{\widetilde H}_t)}{\partial \mathbf{H}_{t-1}}\\ &= \frac{\partial (\mathbf{Z}_t\odot\mathbf{H}_{t-1})}{\partial \mathbf{H}_{t-1}} + \frac{\partial \Big((1-\mathbf{Z}_t)\odot\mathbf{\widetilde H}_t\Big)}{\partial \mathbf{H}_{t-1}} \end{aligned} \] 其中, \[ \begin{aligned} \frac{\partial (\mathbf{Z}_t\odot\mathbf{H}_{t-1})}{\partial \mathbf{H}_{t-1}} &= \mathbf{H}_{t-1}\odot\frac{\partial \mathbf{Z}_t}{\partial \mathbf{H}_{t-1}} + \mathbf{Z}_t \end{aligned} \]

\[ \frac{\partial \Big((1-\mathbf{Z}_t)\odot\mathbf{\widetilde H}_t\Big)}{\partial \mathbf{H}_{t-1}} = (1-\mathbf{Z}_t)\odot\frac{\partial \mathbf{\widetilde H}_t}{\partial \mathbf{H}_{t-1}} + \mathbf{\widetilde H}_t\odot\frac{\partial (1-\mathbf{Z}_t)}{\partial \mathbf{H}_{t-1}} \] 因此, \[ \frac{\partial \mathbf{H}_t}{\partial \mathbf{H}_{t-1}} = \mathbf{H}_{t-1}\odot\frac{\partial \mathbf{Z}_t}{\partial \mathbf{H}_{t-1}} + \mathbf{Z}_t + (1-\mathbf{Z}_t)\odot\frac{\partial \mathbf{\widetilde H}_t}{\partial \mathbf{H}_{t-1}} + \mathbf{\widetilde H}_t\odot\frac{\partial (1-\mathbf{Z}_t)}{\partial \mathbf{H}_{t-1}} \]

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import torch
import torch.nn as nn

class GRUModel(nn.Module):
def __init__(self, vocab_size, embed_size, hidden_size, num_layers=1, batch_first=True):
super().__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers

self.embedding = nn.Embedding(vocab_size, embed_size)
self.gru = nn.GRU(embed_size, hidden_size, num_layers, batch_first=batch_first)
self.fc = nn.Linear(hidden_size, vocab_size)

def forward(self, x, hidden):
x = self.embedding(x)
out, hidden = self.gru(x, hidden)
out = self.fc(out)
return out, hidden

def init_hidden(self, batch_size):
return torch.zeros(self.num_layers, batch_size, self.hidden_size)


GRU模型的定义与RNN类似:

1
nn.GRU()

参数:

  • input_size:输入特征的维度,例如词嵌入后的向量维度或传感器数据的特征数
  • hidden_size:隐藏层神经元的数量,决定模型捕捉序列模式的能力
  • num_layers:默认为1,GRU 的堆叠层数,多层结构可增强模型复杂度
  • batch_first:默认为True,参考RNN
  • bidirectional:默认为False,是否启用双向 GRU,双向结构能同时捕捉前向和后向序列依赖
  • dropout:默认为0,在非最后一层的 GRU 层间添加 Dropout 层防止过拟合(范围:0-1)
  • bias:默认为True,是否使用偏置项

使用:

1
2
3
4
5
# 传入初始隐藏层h0
output, hidden = gru(inputs, h0)

# 不传入隐藏层,自动将初始隐藏状态初始化为全零张量
output, hidden = gru(inputs)

LSTM

引入

长短期记忆网络的设计灵感来自于计算机的逻辑门。长短期记忆网络引入了记忆元(memory cell),或简称为单元(cell)。有些文献认为记忆元是隐状态的一种特殊类型,它们与隐状态具有相同的形状,其设计目的是用于记录附加的信息。为了控制记忆元,我们需要许多门。

模型

输入门、忘记门和输出门

\(t\)时间步的输入为\(\mathbf{X}_t\in\mathbb{R}^{n\times d}\)(样本个数\(n\),输入个数\(d\)),隐藏状态\(\mathbf{H}_t\in\mathbb{R}^{n\times h}\)(隐藏单元个数\(h\)),输入门是\(\mathbf{I}_t\in\mathbb{R}^{n\times h}\),遗忘门是\(\mathbf{F}_t\in\mathbb{R}^{n\times h}\),输出门是\(\mathbf{O}_t\in\mathbb{R}^{n\times h}\)。它们的计算方法如下: \[ \begin{aligned} &\mathbf{I}_t = \sigma(\mathbf{X}_t\mathbf{W}_{xi} + \mathbf{H}_{t-1}\mathbf{W}_{hi} + \mathbf{b}_i)\\ &\mathbf{F}_t = \sigma(\mathbf{X}_t\mathbf{W}_{xf} + \mathbf{H}_{t-1}\mathbf{W}_{hf} + \mathbf{b}_f)\\ &\mathbf{O}_t = \sigma(\mathbf{X}_t\mathbf{W}_{xo} + \mathbf{H}_{t-1}\mathbf{W}_{ho} + \mathbf{b}_o) \end{aligned} \] 其中,\(\mathbf{W}_{xi}, \mathbf{W}_{xf}, \mathbf{W}_{xo}\in\mathbb{R}^{d\times h}\)\(\mathbf{W}_{hi}, \mathbf{W}_{hf}, \mathbf{W}_{ho}\in\mathbb{R}^{h\times h}\)是权重参数,\(\mathbf{b}_i, \mathbf{b}_f, \mathbf{b}_o\in\mathbb{R}^{1\times h}\)是偏置参数

候选记忆元

\[ \mathbf{\widetilde C}_t = \tanh(\mathbf{X}_t\mathbf{W}_{xc} + \mathbf{H}_{t-1}\mathbf{W}_{hc} + \mathbf{b}_c) \] 候选记忆元(candidate memory cell)\(\mathbf{\widetilde C}_t\in\mathbb{R}^{n\times h}\),函数的取值范围是\((-1, 1)\)

其中\(\mathbf{W}_{xc}\in\mathbb{R}^{d\times h}\)\(\mathbf{W}_{hc}\in\mathbb{R}^{h\times h}\)是权重参数,\(\mathbf{b}_c\in\mathbb{R}^{1\times h}\)是偏置参数

记忆元

\[ \mathbf{C}_t = \mathbf{F}_t\odot\mathbf{C}_{t-1} + \mathbf{I}_t\odot\mathbf{\widetilde C}_t \] 输入门\(\mathbf{I}_t\)控制采用多少来自\(\mathbf{\widetilde C}_t\)的新数据,而遗忘门\(\mathbf{F}_t\)控制保留多少过去的记忆元\(\mathbf{C}_t\)的内容

如果遗忘门始终为1且输入门始终为0,则过去的记忆元\(\mathbf{C}_{t-1}\)将随时间被保存并传递到当前时间步。引入这种设计是为了缓解梯度消失的问题,并更好地捕获序列中的长距离依赖关系。

隐状态

\[ \mathbf{H}_t = \mathbf{O}_t\odot\tanh(\mathbf{C}_t) \] 只要输出门接近1,我们就能够有效地将所有记忆信息传递给预测部分,而对于输出门接近0,我们仅保留记忆元内的所有信息,而不需要更新隐藏状态。

前向传播

\[ \begin{aligned} &\mathbf{I}_t = \sigma(\mathbf{X}_t\mathbf{W}_{xi} + \mathbf{H}_{t-1}\mathbf{W}_{hi} + \mathbf{b}_i)\\ &\mathbf{F}_t = \sigma(\mathbf{X}_t\mathbf{W}_{xf} + \mathbf{H}_{t-1}\mathbf{W}_{hf} + \mathbf{b}_f)\\ &\mathbf{O}_t = \sigma(\mathbf{X}_t\mathbf{W}_{xo} + \mathbf{H}_{t-1}\mathbf{W}_{ho} + \mathbf{b}_o)\\ &\mathbf{\widetilde C}_t = \tanh(\mathbf{X}_t\mathbf{W}_{xc} + \mathbf{H}_{t-1}\mathbf{W}_{hc} + \mathbf{b}_c)\\ &\mathbf{C}_t = \mathbf{F}_t\odot\mathbf{C}_{t-1} + \mathbf{I}_t\odot\mathbf{\widetilde C}_t\\ &\mathbf{H}_t = \mathbf{O}_t\odot\tanh(\mathbf{C}_t) \end{aligned} \]

反向传播

LSTM之所以能够记住长期记忆,是因为其可以缓解梯度消失,根据前向传播公式: \[ \begin{aligned} &F_t = \sigma(W_{xf}x_t+W_{hf}h_{t-1}+b_f)\\\ &I_t = \sigma(W_{xi}x_t+W_{hi}h_{t-1}+b_i)\\\ &O_t = \sigma(W_{xo}x_t+W_{ho}h_{t-1}+b_o)\\\ &\stackrel{\sim}C_t = \tanh(W_{xc}x_t+W_{hc}h_{t-1}+b_c)\\\ &C_t = F_t\odot C_{t-1}+I_t\odot \stackrel{\sim}C_t\\\ &h_t = O_t\odot\tanh(C_t) \end{aligned} \] 可得反向传播公式: \[ \begin{aligned} \frac{\partial C_t}{\partial C_{t-1}} = &\frac{\partial (F_t\odot C_{t-1}+I_t\odot\stackrel{\sim}C_t)}{\partial C_{t-1}}\\\ &\begin{aligned} &+\frac{\partial (F_t\odot C_{t-1}+I_t\odot\stackrel{\sim}C_t)}{\partial F_t}\frac{\partial \sigma(W_{xf}x_t+W_{hf}h_{t-1}+b_f)}{\partial h_{t-1}}\frac{\partial (O_{t-1}\odot\tanh(C_{t-1}))}{\partial C_{t-1}}\\\ &+\frac{\partial (F_t\odot C_{t-1}+I_t\odot\stackrel{\sim}C_t)}{\partial I_t}\frac{\partial \sigma(W_{xi}x_t+W_{hi}h_{t-1}+b_i)}{\partial h_{t-1}}\frac{\partial (O_{t-1}\odot\tanh(C_{t-1}))}{\partial C_{t-1}}\\\ &+\frac{\partial (F_t\odot C_{t-1}+I_t\odot\stackrel{\sim}C_t)}{\partial \stackrel{\sim}C_t}\frac{\partial \tanh(W_{xc}x_t+W_{hc}h_{t-1}+b_c)}{\partial h_{t-1}}\frac{\partial (O_{t-1}\odot\tanh(C_{t-1}))}{\partial C_{t-1}} \end{aligned} \end{aligned} \]\(A = \frac{\partial (F_t\odot C_{t-1}+I_t\odot\stackrel{\sim}C_t)}{\partial F_t}\frac{\partial \sigma(W_{xf}x_t+W_{hf}h_{t-1}+b_f)}{\partial h_{t-1}}\frac{\partial (O_{t-1}\odot\tanh(C_{t-1}))}{\partial C_{t-1}}\)\(B = \frac{\partial (F_t\odot C_{t-1}+I_t\odot\stackrel{\sim}C_t)}{\partial I_t}\frac{\partial \sigma(W_{xi}x_t+W_{hi}h_{t-1}+b_i)}{\partial h_{t-1}}\frac{\partial (O_{t-1}\odot\tanh(C_{t-1}))}{\partial C_{t-1}}\)

\(C = \frac{\partial (F_t\odot C_{t-1}+I_t\odot\stackrel{\sim}C_t)}{\partial \stackrel{\sim}C_t}\frac{\partial \tanh(W_{xc}x_t+W_{hc}h_{t-1}+b_c)}{\partial h_{t-1}}\frac{\partial (O_{t-1}\odot\tanh(C_{t-1}))}{\partial C_{t-1}}\)

则: \[ \begin{aligned} \frac{\partial C_t}{\partial C_{t-1}} &= F_t + A+B+C \end{aligned} \] 因为 \[ \begin{aligned} &\frac{\partial (F_t\odot C_{t-1}+I_t\odot\stackrel{\sim}C_t)}{\partial F_t}\frac{\partial \sigma(W_{xf}x_t+W_{hf}h_{t-1}+b_f)}{\partial h_{t-1}}\frac{\partial (O_{t-1}\odot\tanh(C_{t-1}))}{\partial C_{t-1}} = C_{t-1}\sigma_f'(\cdot)W_{hf}\odot O_{t-1}\tanh'(C_{t-1})\\\ &\frac{\partial (F_t\odot C_{t-1}+I_t\odot\stackrel{\sim}C_t)}{\partial I_t}\frac{\partial \sigma(W_{xi}x_t+W_{hi}h_{t-1}+b_i)}{\partial h_{t-1}}\frac{\partial (O_{t-1}\odot\tanh(C_{t-1}))}{\partial C_{t-1}} = \stackrel{\sim}C_t\sigma_i'(\cdot)W_{hi}\odot O_{t-1}\tanh'(C_{t-1})\\\ &\frac{\partial (F_t\odot C_{t-1}+I_t\odot\stackrel{\sim}C_t)}{\partial \stackrel{\sim}C_t}\frac{\partial \tanh(W_{xc}x_t+W_{hc}h_{t-1}+b_c)}{\partial h_{t-1}}\frac{\partial (O_{t-1}\odot\tanh(C_{t-1}))}{\partial C_{t-1}} = I_t\tanh'(\cdot)W_{hc}\odot O_{t-1}\tanh'(C_{t-1}) \end{aligned} \]

所以: \[ \begin{aligned} \frac{\partial C_t}{\partial C_{t-1}} = &F_t\\\ &\begin{aligned} &+ C_{t-1}\sigma_f'(\cdot)W_{hf}\odot O_{t-1}\tanh'(C_{t-1})\\\ &+ \stackrel{\sim}C_t\sigma_i'(\cdot)W_{hi}\odot O_{t-1}\tanh'(C_{t-1})\\\ &+ I_t\tanh'(\cdot)W_{hc}\odot O_{t-1}\tanh'(C_{t-1}) \end{aligned} \end{aligned} \] 如果模型更注重长期记忆,那么遗忘门就会更倾向于打开,即\(F_t\)内元素的值会更大,那么\(\frac{\partial C_t}{\partial C_{t-1}}\)的值就会越大,反向传播在时序上就能走得更远,越难以梯度消失;而如果模型更注重短期记忆,那么遗忘门就会更倾向于关闭,即\(F_t\)内元素的值会更小,那么\(\frac{\partial C_{t}}{\partial C_{t-1}}\)的值就会比较小,反向传播在时序上不能传很远,容易梯度消失。

在LSTM的远古模型中,\(C_t = C_{t-1}+I_t\odot\stackrel{\sim}C_t\),这样可能导致\(C_t\)的值随\(t\)增大而增大,最终导致梯度爆炸。

当然,\(\frac{\partial C_t}{\partial C_{t-1}}\)不只由\(F_t\)决定,它的值也与后面三项有关,因此,如果学习的\(W_{hf}\)\(W_{hi}\)\(W_{hc}\)内元素的值比较大,那么在一定程度上也能缓解梯度消失。

在普通的RNN神经元的反向传播中, \[ \frac{\partial h_t}{\partial w_h} = \frac{\partial f(x_t, h_{t-1}, w_h, b_h)}{\partial w_h} + \displaystyle\sum_{i=1}^{t-1}\bigg(\prod_{j=i+1}^{t}\frac{\partial f(x_j,h_{j-1},w_h, b_h)}{\partial h_{j-1}} \bigg)\frac{\partial f(x_i, h_{i-1}, w_h, b_h)}{\partial w_h}\tag{1} \] 其梯度消失正是由于$_{j=i+1}^{t} \(引起的,也就是由隐藏状态反向传播引起的,而LSTM模型中负责在时间步中传递信息的为\)C_t\((记忆单元),因此会导致梯度消失的就是\)\(,因为其在反向传播时连乘,如果元素的值小于1,连乘就会导致梯度消失,而LSTM正好就解决了\)$内元素值过小的问题(当然,如果模型更注重短期记忆,梯度消失也没有问题)

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import torch
import torch.nn as nn

class LSTMModel(nn.Module):
def __init__(self, vocab_size, embed_size, hidden_size, num_layers=1, batch_first=True):
super().__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers

self.embedding = nn.Embedding(vocab_size, embed_size)
self.lstm = nn.LSTM(embed_size, hidden_size, num_layers=num_layers, batch_first=batch_first)
self.fc = nn.Linear(hidden_size, vocab_size)

def forward(self, x, hidden, c):
x = self.embedding(x)
out, (hidden, c) = self.lstm(x, (hidden, c))
out = self.fc(out)

return out, (hidden, c)

def init_hc(self, batch_size):
h0 = torch.zeros(self.num_layers, batch_size, self.hidden_size)
c0 = torch.zeros_like(h0)
return h0, c0

LSTM模型:

1
nn.LSTM()

参数:

  • input_size:输入张量中每个时间步的特征维度。例如,词嵌入后的向量维度或传感器数据的特征数
  • hidden_size:隐藏层神经元数量,决定模型容量
  • num_layers:默认为1,GRU 的堆叠层数,多层结构可增强模型复杂度
  • batch_first:默认为True,参考RNN
  • bidirectional:默认为False,是否启用双向 GRU,双向结构能同时捕捉前向和后向序列依赖
  • dropout:默认为0,在非最后一层的 GRU 层间添加 Dropout 层防止过拟合(范围:0-1)
  • bias:默认为True,是否使用偏置项

使用:

1
2
3
4
5
# 不传入隐藏层h0和记忆元c0,自动将初始隐藏状态和初始记忆元初始化为全零张量
output, (h_n, c_n) = lstm(packed_input)

# 传入初始隐藏层h0和记忆元c0
output, (hn, cn) = lstm(inputs, (h0, c0))

LSTM和GRU
https://blog.shinebook.net/2025/04/03/人工智能/理论基础/深度学习/LSTM和GRU/
作者
X
发布于
2025年4月3日
许可协议