深度学习学习经验——长短期记忆网络(LSTM)

长短期记忆网络(LSTM)

长短期记忆网络(LSTM,Long Short-Term Memory)是一种特殊的循环神经网络(RNN),专为解决 RNN 中长期依赖问题而设计。LSTM 引入了三个门和一个细胞状态(cell state),以便更好地控制信息的流动,确保网络能够记住长期的依赖关系。我们将通过一个逐步深入的案例来讲解 LSTM 的内部结构和工作机制,并使用 PyTorch 实现一个 LSTM 模型。

1. 问题描述

我上一篇讲解RNN的文章相似,假设我们仍然要预测未来的天气情况,但是这次数据包含更多的噪声,且我们希望模型能够更好地“记住”一段时间内的趋势信息。这时,LSTM 比普通 RNN 更适合这种任务,因为它能够通过门控机制更精确地控制信息流动。

2. LSTM 的基本原理

LSTM 通过引入 输入门(Input Gate)、遗忘门(Forget Gate)、输出门(Output Gate)和 细胞状态(Cell State)来管理信息的记忆和遗忘。

2.1 细胞状态(Cell State)

细胞状态是贯穿整个 LSTM 的主线,类似于一个“传送带”,它能够允许信息在序列中几乎不受干扰地传递下去。LSTM 通过少量的线性相互作用,轻松地让信息在其上流动,只有少数的部分会被门结构所改变。

2.2 遗忘门(Forget Gate)

遗忘门决定了细胞状态中哪些信息需要丢弃。这个门读取当前的输入 ( x t (x_t (xt) 和上一时刻的隐藏状态 ( h t − 1 (h_{t-1} (ht1),并输出一个介于 0 和 1 之间的值,其中 0 代表完全忘记,1 代表完全保留。

f t = σ ( W f ⋅ [ h t − 1 , x t ] + b f ) f_t = \sigma(W_f \cdot [h_{t-1}, x_t] + b_f) ft=σ(Wf[ht1,xt]+bf)

forget_gate = nn.Sigmoid()
f_t = forget_gate(torch.matmul(W_f, torch.cat((h_{t-1}, x_t), dim=1)) + b_f)
2.3 输入门(Input Gate)

输入门控制哪些新的信息将被写入细胞状态。这个过程分为两步:首先,输入门生成一个控制写入的信号,然后通过一个 tanh 层创建一个新的候选细胞状态。

i t = σ ( W i ⋅ [ h t − 1 , x t ] + b i ) i_t = \sigma(W_i \cdot [h_{t-1}, x_t] + b_i) it=σ(Wi[ht1,xt]+bi)

C ~ t = tanh ⁡ ( W C ⋅ [ h t − 1 , x t ] + b C ) \tilde{C}_t = \tanh(W_C \cdot [h_{t-1}, x_t] + b_C) C~t=tanh(WC[ht1,xt]+bC)

input_gate = nn.Sigmoid()
i_t = input_gate(torch.matmul(W_i, torch.cat((h_{t-1}, x_t), dim=1)) + b_i)

candidate_layer = nn.Tanh()
C_tilda = candidate_layer(torch.matmul(W_C, torch.cat((h_{t-1}, x_t), dim=1)) + b_C)
2.4 更新细胞状态(Cell State)

在更新细胞状态时,我们结合了遗忘门的结果 ( f t (f_t (ft) 和输入门的结果 ( i t (i_t (it) 以及候选细胞状态 ( C ~ t (\tilde{C}_t (C~t):

C t = f t ∗ C t − 1 + i t ∗ C ~ t C_t = f_t * C_{t-1} + i_t * \tilde{C}_t Ct=ftCt1+itC~t

C_t = f_t * C_{t-1} + i_t * C_tilda
2.5 输出门(Output Gate)

输出门决定了当前的隐藏状态 ( h t (h_t (ht) 是什么,同时输出门还控制了有多少细胞状态信息能够传递到下一层。首先,输出门生成一个信号,然后结合当前的细胞状态生成新的隐藏状态。

o t = σ ( W o ⋅ [ h t − 1 , x t ] + b o ) o_t = \sigma(W_o \cdot [h_{t-1}, x_t] + b_o) ot=σ(Wo[ht1,xt]+bo)

h t = o t ∗ tanh ⁡ ( C t ) h_t = o_t * \tanh(C_t) ht=ottanh(Ct)

output_gate = nn.Sigmoid()
o_t = output_gate(torch.matmul(W_o, torch.cat((h_{t-1}, x_t), dim=1)) + b_o)

h_t = o_t * torch.tanh(C_t)

3. 使用 PyTorch 实现 LSTM

现在我们使用 PyTorch 实现一个 LSTM 模型,并用它来预测天气。

3.1 导入必要的库
import torch
import torch.nn as nn
import numpy as np
3.2 定义 LSTM 模型
class WeatherLSTM(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(WeatherLSTM, self).__init__()
        self.hidden_size = hidden_size
        self.lstm = nn.LSTM(input_size, hidden_size, batch_first=True)
        self.fc = nn.Linear(hidden_size, output_size)
    
    def forward(self, x):
        h_0 = torch.zeros(1, x.size(0), self.hidden_size)  # 初始化隐藏状态
        c_0 = torch.zeros(1, x.size(0), self.hidden_size)  # 初始化细胞状态
        out, (h_n, c_n) = self.lstm(x, (h_0, c_0))  # 计算所有时间步长的输出
        out = self.fc(out[:, -1, :])  # 取最后一个时间步长的输出并通过线性层
        return out

在这个模型中,LSTM 层代替了 RNN 层,它自动处理了前面介绍的遗忘门、输入门、输出门和细胞状态的更新。

3.3 准备数据
# 生成示例数据
data = np.array([[30, 31, 32, 33, 34],
                 [32, 33, 34, 35, 36],
                 [35, 36, 37, 38, 39]],
                 dtype=np.float32)

labels = np.array([35, 37, 40], dtype=np.float32)  # 对应的目标值

# 转换为 PyTorch 张量
data = torch.from_numpy(data).unsqueeze(-1)  # 添加特征维度
labels = torch.from_numpy(labels).unsqueeze(-1)
3.4 训练模型
# 定义超参数
input_size = 1
hidden_size = 10
output_size = 1
num_epochs = 100
learning_rate = 0.01

# 实例化模型
model = WeatherLSTM(input_size, hidden_size, output_size)

# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

# 训练模型
for epoch in range(num_epochs):
    model.train()
    outputs = model(data)
    loss = criterion(outputs, labels)
    
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    if (epoch+1) % 10 == 0:
        print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
3.5 使用模型进行预测
model.eval()
with torch.no_grad():
    test_input = torch.tensor([[36, 37, 38, 39, 40]], dtype=torch.float32).unsqueeze(-1)
    predicted_temperature = model(test_input)
    print(f"预测的温度: {predicted_temperature.item():.2f}")

4. 完整代码

import torch
import torch.nn as nn
import numpy as np

class WeatherLSTM(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(WeatherLSTM, self).__init__()
        self.hidden_size = hidden_size
        self.lstm = nn.LSTM(input_size, hidden_size, batch_first=True)
        self.fc = nn.Linear(hidden_size, output_size)
    
    def forward(self, x):
        h_0 = torch.zeros(1, x.size(0), self.hidden_size)
        c_0 = torch.zeros(1, x.size(0), self.hidden_size)
        out, (h_n, c_n) = self.lstm(x, (h_0, c_0))
        out = self.fc(out[:, -1, :])
        return out

# 生成示例数据
data = np.array([[30, 31, 32, 33, 34],
                 [32, 33, 34, 35, 36],
                 [35, 36, 37, 38, 39]],
                 dtype=np.float32)

labels = np.array([35, 37, 40], dtype=np.float32)

data = torch.from_numpy(data).unsqueeze(-1)
labels = torch.from_numpy(labels).unsqueeze(-1)

# 定义超参数
input_size = 1
hidden_size = 10
output_size = 1
num_epochs = 100
learning_rate = 0.01

# 实例化模型
model = WeatherLSTM(input_size, hidden_size, output_size)

# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

# 训练模型
for epoch in range(num_epochs):
    model.train()
    outputs = model(data)
    loss = criterion(outputs, labels)
    
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    if (epoch+1) % 10 == 0:
        print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')

# 进行预测
model.eval()
with torch.no_grad():
    test_input = torch.tensor([[36, 37, 38, 39, 40]], dtype=torch.float32).unsqueeze(-1)
    predicted_temperature = model(test_input)
    print(f"预测的温度: {predicted_temperature.item():.2f}")

5. 总结

LSTM 通过引入遗忘门、输入门、输出门和细胞状态,能够有效解决 RNN 中的长期依赖问题,使得模型可以更好地在序列数据中保留重要信息并进行预测。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linductor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值