可视化方式,彻底搞懂LSTM
机器学习算法与Python实战
共 5397字,需浏览 11分钟
· 2022-04-26
来自 | 知乎 作者 | master苏
链接 | https://zhuanlan.zhihu.com/p/139617364
1、传统的BP网络和CNN网络
2、LSTM网络
3、LSTM的输入结构
4、pytorch中的LSTM
4.1 pytorch中定义的LSTM模型
4.2 喂给LSTM的数据格式
4.3 LSTM的output格式
5、LSTM和其他网络组合
![](https://filescdn.proginn.com/796036b9c9fc4e86007451936b752ed4/3a9871089e45109aac2f28b4b32f2667.webp)
![](https://filescdn.proginn.com/93c76c522c1ac6610705cfaecf3a8988/62e85d23034d985d8e6270ddd4c82ba8.webp)
# 示例代码,没有实际意义
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu')) # 添加卷积层
model.add(MaxPooling2D(pool_size=(2, 2))) # 添加池化层
model.add(Dropout(0.25)) # 添加dropout层
model.add(Conv2D(32, (3, 3), activation='relu')) # 添加卷积层
model.add(MaxPooling2D(pool_size=(2, 2))) # 添加池化层
model.add(Dropout(0.25)) # 添加dropout层
.... # 添加其他卷积操作
model.add(Flatten()) # 拉平三维数组为2维数组
model.add(Dense(256, activation='relu')) 添加普通的全连接层
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
.... # 训练网络
![](https://filescdn.proginn.com/2dcc4922234b40ac7a91bb6cc4f789c3/a4b7c919ca8c1a96027d3547c4ae2382.webp)
![](https://filescdn.proginn.com/8c85fc6158b7727a2d000dd81bedf4e9/152a9e142319c1a6a4164294086047ed.webp)
![](https://filescdn.proginn.com/3f739a9702c3ab654c821e63ff1497e1/5c61c2506592541972b7857c3d41b009.webp)
![](https://filescdn.proginn.com/43d18ff254c77c68e4b869b5856a9aa9/b172a71cc06c240171b40e60205a3ccc.webp)
![](https://filescdn.proginn.com/ca9a1ccacfaa5571c6b5c404e39852ca/ed4a977bd50bbb6157c58ec90c8dbaab.webp)
![](https://filescdn.proginn.com/5f3e35a24a2515a25c5bd312f7e0fd04/c662bef2e7bbd7e9b76d9bbe1a8150de.webp)
4.1 pytorch中定义的LSTM模型
class torch.nn.LSTM(*args, **kwargs)
参数有:
input_size:x的特征维度
hidden_size:隐藏层的特征维度
num_layers:lstm隐层的层数,默认为1
bias:False则bihbih=0和bhhbhh=0. 默认为True
batch_first:True则输入输出的数据格式为 (batch, seq, feature)
dropout:除最后一层,每一层的输出都进行dropout,默认为: 0
bidirectional:True则为双向lstm默认为False
![](https://filescdn.proginn.com/02d4f3f0457cf1dbc9928d8cce1e6a3f/2a9b51ba518b4de4e82fe1d476847f78.webp)
![](https://filescdn.proginn.com/67c8ffbe8424afc656c817552dcd193f/db41c23dcf3d0889cab9b69ccbb4f580.webp)
![](https://filescdn.proginn.com/65075700c331ee05edc42204076ec3cd/523e7d11cd6c9eeec0d7c23ec710647c.webp)
4.2 喂给LSTM的数据格式
input(seq_len, batch, input_size)
参数有:
seq_len:序列长度,在NLP中就是句子长度,一般都会用pad_sequence补齐长度
batch:每次喂给网络的数据条数,在NLP中就是一次喂给网络多少个句子
input_size:特征维度,和前面定义网络结构的input_size一致。
input(batch, seq_len, input_size)
![](https://filescdn.proginn.com/ab6d3e9eded35e4b6489c125e7f8eb54/1654d0d8575d34672f8d3163abbe4c1e.webp)
![](https://filescdn.proginn.com/16b1f5a8e126fab9e0c85f1b015d1b14/1e5e7d63cda3cad8b6e06a395931c549.webp)
![](https://filescdn.proginn.com/af9d8ee5913247d823440a16826a0898/2fd67db47caf5b539060199537eedbee.webp)
h0(num_layers * num_directions, batch, hidden_size)
c0(num_layers * num_directions, batch, hidden_size)
参数:
num_layers:隐藏层数
num_directions:如果是单向循环网络,则num_directions=1,双向则num_directions=2
batch:输入数据的batch
hidden_size:隐藏层神经元个数
input(batch, seq_len, input_size)
h0(batc,num_layers * num_directions, h, hidden_size)
c0(batc,num_layers * num_directions, h, hidden_size)
4.3 LSTM的output格式
output,(ht, ct) = net(input)
output: 最后一个状态的隐藏层的神经元输出
ht:最后一个状态的隐含层的状态值
ct:最后一个状态的隐含层的遗忘门值
output(seq_len, batch, hidden_size * num_directions)
ht(num_layers * num_directions, batch, hidden_size)
ct(num_layers * num_directions, batch, hidden_size)
input(batch, seq_len, input_size)
ht(batc,num_layers * num_directions, h, hidden_size)
ct(batc,num_layers * num_directions, h, hidden_size)
![](https://filescdn.proginn.com/023c492307fb34459ff2db7fe6f8106e/45db2c1047e2c83d035a271aafa42bce.webp)
![](https://filescdn.proginn.com/393e9c1a94aa7a16268e7e8c59e93a7e/449d333d08bb37cbbd730cebe5b57a8e.webp)
![](https://filescdn.proginn.com/9cbd3e5b8ce6e36df26397e42c2888f4/8ffc417568f68c9a3bcbdd90600e2ef5.webp)
import torch
from torch import nn
class RegLSTM(nn.Module):
def __init__(self):
super(RegLSTM, self).__init__()
# 定义LSTM
self.rnn = nn.LSTM(input_size, hidden_size, hidden_num_layers)
# 定义回归层网络,输入的特征维度等于LSTM的输出,输出维度为1
self.reg = nn.Sequential(
nn.Linear(hidden_size, 1)
)
def forward(self, x):
x, (ht,ct) = self.rnn(x)
seq_len, batch_size, hidden_size= x.shape
x = y.view(-1, hidden_size)
x = self.reg(x)
x = x.view(seq_len, batch_size, -1)
return x
推荐阅读
(点击标题可跳转阅读)
老铁,三连支持一下,好吗?↓↓↓
评论