PyTorch深度学习LSTM从input输入到Linear输出
作者:Cyril_KI 发布时间:2022-04-03 23:11:32
LSTM介绍
关于LSTM的具体原理,可以参考:
https://www.jb51.net/article/178582.htm
https://www.jb51.net/article/178423.htm
系列文章:
PyTorch搭建双向LSTM实现时间序列负荷预测
PyTorch搭建LSTM实现多变量多步长时序负荷预测
PyTorch搭建LSTM实现多变量时序负荷预测
PyTorch搭建LSTM实现时间序列负荷预测
LSTM参数
关于nn.LSTM的参数,官方文档给出的解释为:
总共有七个参数,其中只有前三个是必须的。由于大家普遍使用PyTorch的DataLoader来形成批量数据,因此batch_first也比较重要。LSTM的两个常见的应用场景为文本处理和时序预测,因此下面对每个参数我都会从这两个方面来进行具体解释。
input_size:在文本处理中,由于一个单词没法参与运算,因此我们得通过Word2Vec来对单词进行嵌入表示,将每一个单词表示成一个向量,此时input_size=embedding_size。
比如每个句子中有五个单词,每个单词用一个100维向量来表示,那么这里input_size=100;
在时间序列预测中,比如需要预测负荷,每一个负荷都是一个单独的值,都可以直接参与运算,因此并不需要将每一个负荷表示成一个向量,此时input_size=1。
但如果我们使用多变量进行预测,比如我们利用前24小时每一时刻的[负荷、风速、温度、压强、湿度、天气、节假日信息]来预测下一时刻的负荷,那么此时input_size=7。
hidden_size:隐藏层节点个数。可以随意设置。
num_layers:层数。nn.LSTMCell与nn.LSTM相比,num_layers默认为1。
batch_first:默认为False,意义见后文。
Inputs
关于LSTM的输入,官方文档给出的定义为:
可以看到,输入由两部分组成:input、(初始的隐状态h_0,初始的单元状态c_0)
其中input:
input(seq_len, batch_size, input_size)
seq_len:在文本处理中,如果一个句子有7个单词,则seq_len=7;在时间序列预测中,假设我们用前24个小时的负荷来预测下一时刻负荷,则seq_len=24。
batch_size:一次性输入LSTM中的样本个数。在文本处理中,可以一次性输入很多个句子;在时间序列预测中,也可以一次性输入很多条数据。
input_size:见前文。
(h_0, c_0):
h_0(num_directions * num_layers, batch_size, hidden_size)
c_0(num_directions * num_layers, batch_size, hidden_size)
h_0和c_0的shape一致。
num_directions:如果是双向LSTM,则num_directions=2;否则num_directions=1。
num_layers:见前文。
batch_size:见前文。
hidden_size:见前文。
Outputs
关于LSTM的输出,官方文档给出的定义为:
可以看到,输出也由两部分组成:otput、(隐状态h_n,单元状态c_n)
其中output的shape为:
output(seq_len, batch_size, num_directions * hidden_size)
h_n和c_n的shape保持不变,参数解释见前文。
batch_first
如果在初始化LSTM时令batch_first=True,那么input和output的shape将由:
input(seq_len, batch_size, input_size)
output(seq_len, batch_size, num_directions * hidden_size)
变为:
input(batch_size, seq_len, input_size)
output(batch_size, seq_len, num_directions * hidden_size)
即batch_size提前。
案例
简单搭建一个LSTM如下所示:
class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size, batch_size):
super().__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.output_size = output_size
self.num_directions = 1 # 单向LSTM
self.batch_size = batch_size
self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, batch_first=True)
self.linear = nn.Linear(self.hidden_size, self.output_size)
def forward(self, input_seq):
h_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(device)
c_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(device)
seq_len = input_seq.shape[1] # (5, 30)
# input(batch_size, seq_len, input_size)
input_seq = input_seq.view(self.batch_size, seq_len, 1) # (5, 30, 1)
# output(batch_size, seq_len, num_directions * hidden_size)
output, _ = self.lstm(input_seq, (h_0, c_0)) # output(5, 30, 64)
output = output.contiguous().view(self.batch_size * seq_len, self.hidden_size) # (5 * 30, 64)
pred = self.linear(output) # pred(150, 1)
pred = pred.view(self.batch_size, seq_len, -1) # (5, 30, 1)
pred = pred[:, -1, :] # (5, 1)
return pred
其中定义模型的代码为:
self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, batch_first=True)
self.linear = nn.Linear(self.hidden_size, self.output_size)
我们加上具体的数字:
self.lstm = nn.LSTM(self.input_size=1, self.hidden_size=64, self.num_layers=5, batch_first=True)
self.linear = nn.Linear(self.hidden_size=64, self.output_size=1)
再看前向传播:
def forward(self, input_seq):
h_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(device)
c_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(device)
seq_len = input_seq.shape[1] # (5, 30)
# input(batch_size, seq_len, input_size)
input_seq = input_seq.view(self.batch_size, seq_len, 1) # (5, 30, 1)
# output(batch_size, seq_len, num_directions * hidden_size)
output, _ = self.lstm(input_seq, (h_0, c_0)) # output(5, 30, 64)
output = output.contiguous().view(self.batch_size * seq_len, self.hidden_size) # (5 * 30, 64)
pred = self.linear(output) # (150, 1)
pred = pred.view(self.batch_size, seq_len, -1) # (5, 30, 1)
pred = pred[:, -1, :] # (5, 1)
return pred
假设用前30个预测下一个,则seq_len=30,batch_size=5,由于设置了batch_first=True,因此,输入到LSTM中的input的shape应该为:
input(batch_size, seq_len, input_size) = input(5, 30, 1)
但实际上,经过DataLoader处理后的input_seq为:
input_seq(batch_size, seq_len) = input_seq(5, 30)
(5, 30)表示一共5条数据,每条数据的维度都为30。为了匹配LSTM的输入,我们需要对input_seq的shape进行变换:
input_seq = input_seq.view(self.batch_size, seq_len, 1) # (5, 30, 1)
然后将input_seq送入LSTM:
output, _ = self.lstm(input_seq, (h_0, c_0)) # output(5, 30, 64)
根据前文,output的shape为:
output(batch_size, seq_len, num_directions * hidden_size) = output(5, 30, 64)
全连接层的定义为:
self.linear = nn.Linear(self.hidden_size=64, self.output_size=1)
因此,我们需要将output的第二维度变换为64(150, 64):
output = output.contiguous().view(self.batch_size * seq_len, self.hidden_size) # (5 * 30, 64)
然后将output送入全连接层:
pred = self.linear(output) # pred(150, 1)
得到的预测值shape为(150, 1)。我们需要将其进行还原,变成(5, 30, 1):
pred = pred.view(self.batch_size, seq_len, -1) # (5, 30, 1)
在用DataLoader处理了数据后,得到的input_seq和label的shape分别为:
input_seq(batch_size, seq_len) = input_seq(5, 30)label(batch_size, output_size) = label(5, 1)
由于输出是输入右移,我们只需要取pred第二维度(time)中的最后一个数据:
pred = pred[:, -1, :] # (5, 1)
这样,我们就得到了预测值,然后与label求loss,然后再反向更新参数即可。
时间序列预测的一个真实案例请见:PyTorch搭建LSTM实现时间序列预测(负荷预测)
来源:https://blog.csdn.net/Cyril_KI/article/details/122557880


猜你喜欢
- 这是支持的下载版本,去官网下载2020.3及以上(2021-03-18测试破解有效)官网下载地址:https://www.jetbrains
- 对于大多数web应用来说,数据库都是一个十分基础性的部分。如果你在使用PHP,那么你很可能也在使用MySQL—LAMP系列中举足轻重的一份子
- 定义一个banner.js文件,代码如下;window.requestAnimationFrame = window.requestAnim
- 1.文件写入#打开文件,路径不对会报错f = open(r"C:\Users\jm\Desktop\pyfile.txt"
- python读写文件有时候会出现 ‘XXX'编码不能打开XXX什么的,用记事本打开要读取的文件,另存为UTF
- 一、Tesseract文字识别是ORC的一部分内容,ORC的意思是光学字符识别,通俗讲就是文字识别。Tesseract是一个用于文字识别的工
- 生命游戏的算法就不多解释了,百度一下介绍随处可见。因为网上大多数版本都是基于pygame,matlab等外部库实现的,二维数组大多是用num
- 一、Beautiful Soup的安装Beautiful Soup是Python的一个HTML或XML的解析库,使用它可以很方便地从网页中提
- 本文实例讲述了Python with语句上下文管理器。分享给大家供大家参考,具体如下:在编程中会经常碰到这种情况:有一个特殊的语句块,在执行
- 尽管可能是个比较老的话题了,但是我还是从来没有整理过。今天在《精通HTML》一书中看到,这里整理一下。在XHTML中,<html>
- 日常工作生活中,事情一多,就会忘记一些该做未做的事情。即使有时候把事情记录在了小本本上或者手机、电脑端备忘录上,也总会有查看不及时,导致错过
- 一、CrawlSpider类介绍1.1 引入使用scrapy框架进行全站数据爬取可以基于Spider类,也可以使用接下来用到的CrawlSp
- 之前说过要聊聊 干职业设计经理的活 的问题,貌似有些朋友对这个事情还挺关心的,我理解为一方面是掌握对付猎头时候的标准答案,一方面是
- 首先,需要简单的了解一下爬虫,尽可能简单快速的上手,其次,需要了解的是百度的API的接口,搞定这个之后,最后,按照官方给出的demo,然后写
- 使用python的subprocess模块实现对SVN的相关操作。设置GitSvn类,在该类下自定义执行SVN常规操作的方法。SVN的常规操
- 配置数据库密码特殊字符报错一般的springboot项目会有application.yml或者application.properties文
- 背景要做IP地址归属地查询,量比较大,所以想先从网上找到大部分的分配数据,写个蜘蛛程序来抓取入库,以后在程序的运行中不断进行维护、更新、完善
- 要用django的orm表达sql的exists子查询,是个比较麻烦的事情,需要做两部来完成from django.db.models im
- matplotlib官方文档:https://matplotlib.org/stable/users/index.htmlmatplotli
- 一、效果图二、必要工具Python3.7pycharm2019再然后配置它的文件,设置游戏屏幕的大小,图片路径。代码如下''