pytorch 中LSTM模型获取最后一层的输出结果,单向或双向
单向LSTM
import torch.nn as nn
import torch seq_len = 20
batch_size = 64
embedding_dim = 100
num_embeddings = 300
hidden_size = 128
number_layer = 3 input = torch.randint(low=0,high=256,size=[batch_size,seq_len]) #[64,20] embedding = nn.Embedding(num_embeddings,embedding_dim) input_embeded = embedding(input) #[64,20,100] #转置,变换batch_size 和seq_len
# input_embeded = input_embeded.transpose(0,1)
# input_embeded = input_embeded.permute(1,0,2)
#实例化lstm lstm = nn.LSTM(input_size=embedding_dim,hidden_size=hidden_size,batch_first=True,num_layers=number_layer) output,(h_n,c_n) = lstm(input_embeded)
print(output.size()) #[64,20,128] [batch_size,seq_len,hidden_size]
print(h_n.size()) #[3,64,128] [number_layer,batch_size,hidden_size]
print(c_n.size()) #同上 #获取最后时间步的output
output_last = output[:,-1,:]
#获取最后一层的h_n
h_n_last = h_n[-1] print(output_last.size())
print(h_n_last.size())
#最后的output等于最后一层的h_n
print(output_last.eq(h_n_last))
D:\anaconda\python.exe C:/Users/liuxinyu/Desktop/pytorch_test/day4/LSTM练习.py
torch.Size([64, 20, 128])
torch.Size([3, 64, 128])
torch.Size([3, 64, 128])
torch.Size([64, 128])
torch.Size([64, 128])
tensor([[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
...,
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True]])
Process finished with exit code 0
双向LSTM
import torch.nn as nn
import torch seq_len = 20
batch_size = 64
embedding_dim = 100
num_embeddings = 300
hidden_size = 128
number_layer = 3 input = torch.randint(low=0,high=256,size=[batch_size,seq_len]) #[64,20] embedding = nn.Embedding(num_embeddings,embedding_dim) input_embeded = embedding(input) #[64,20,100] #转置,变换batch_size 和seq_len
# input_embeded = input_embeded.transpose(0,1)
# input_embeded = input_embeded.permute(1,0,2)
#实例化lstm lstm = nn.LSTM(input_size=embedding_dim,hidden_size=hidden_size,batch_first=True,num_layers=number_layer,bidirectional=True) output,(h_n,c_n) = lstm(input_embeded)
print(output.size()) #[64,20,128*2] [batch_size,seq_len,hidden_size]
print(h_n.size()) #[3*2,64,128] [number_layer,batch_size,hidden_size]
print(c_n.size()) #同上 #获取反向的最后一个output
output_last = output[:,0,-128:]
#获反向最后一层的h_n
h_n_last = h_n[-1] print(output_last.size())
print(h_n_last.size())
# 反向最后的output等于最后一层的h_n
print(output_last.eq(h_n_last)) #获取正向的最后一个output
output_last = output[:,-1,:128]
#获取正向最后一层的h_n
h_n_last = h_n[-2]
# 反向最后的output等于最后一层的h_n
print(output_last.eq(h_n_last))
D:\anaconda\python.exe C:/Users/liuxinyu/Desktop/pytorch_test/day4/双向LSTM练习.py
torch.Size([64, 20, 256])
torch.Size([6, 64, 128])
torch.Size([6, 64, 128])
torch.Size([64, 128])
torch.Size([64, 128])
tensor([[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
...,
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True]])
tensor([[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
...,
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True],
[True, True, True, ..., True, True, True]])
Process finished with exit code 0
pytorch 中LSTM模型获取最后一层的输出结果,单向或双向的更多相关文章
- PyTorch中的Batch Normalization
Pytorch中的BatchNorm的API主要有: 1 torch.nn.BatchNorm1d(num_features, 2 3 eps=1e-05, 4 5 momentum=0.1, 6 7 ...
- Python中利用LSTM模型进行时间序列预测分析
时间序列模型 时间序列预测分析就是利用过去一段时间内某事件时间的特征来预测未来一段时间内该事件的特征.这是一类相对比较复杂的预测建模问题,和回归分析模型的预测不同,时间序列模型是依赖于事件发生的先后顺 ...
- 详解Pytorch中的网络构造,模型save和load,.pth权重文件解析
转载:https://zhuanlan.zhihu.com/p/53927068 https://blog.csdn.net/wangdongwei0/article/details/88956527 ...
- PyTorch中使用深度学习(CNN和LSTM)的自动图像标题
介绍 深度学习现在是一个非常猖獗的领域 - 有如此多的应用程序日复一日地出现.深入了解深度学习的最佳方法是亲自动手.尽可能多地参与项目,并尝试自己完成.这将帮助您更深入地掌握主题,并帮助您成为更好的深 ...
- Pytorch的LSTM的理解
class torch.nn.LSTM(*args, **kwargs) 参数列表 input_size:x的特征维度 hidden_size:隐藏层的特征维度 num_layers:lstm隐层的层 ...
- 转pytorch中训练深度神经网络模型的关键知识点
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/weixin_42279044/articl ...
- 【小白学PyTorch】6 模型的构建访问遍历存储(附代码)
文章转载自微信公众号:机器学习炼丹术.欢迎大家关注,这是我的学习分享公众号,100+原创干货. 文章目录: 目录 1 模型构建函数 1.1 add_module 1.2 ModuleList 1.3 ...
- pytorch中网络特征图(feture map)、卷积核权重、卷积核最匹配样本、类别激活图(Class Activation Map/CAM)、网络结构的可视化方法
目录 0,可视化的重要性: 1,特征图(feture map) 2,卷积核权重 3,卷积核最匹配样本 4,类别激活图(Class Activation Map/CAM) 5,网络结构的可视化 0,可视 ...
- LSTM模型与前向反向传播算法
在循环神经网络(RNN)模型与前向反向传播算法中,我们总结了对RNN模型做了总结.由于RNN也有梯度消失的问题,因此很难处理长序列的数据,大牛们对RNN做了改进,得到了RNN的特例LSTM(Long ...
随机推荐
- 编译原理:DFA最小化,语法分析初步
1.将DFA最小化:教材P65 第9题 解析: 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 解析: S→ 0A|1B →S → 0(1S|1)|1(0S|0 ...
- Python IDE ——Anaconda+PyCharm的安装与配置
一 前言 最近莫名其妙地想学习一下Python,想着利用业余时间学习一下机器学习(或许仅仅是脑子一热吧).借着研究生期间对于PyCharm安装的印象,在自己的电脑上重新又安装了一遍.利用周末的一点时间 ...
- Fedora20在神州战神K650D1安装过程,使用netinstall和Dvd.iso镜像安装。
最近新买一笔记本,神州战神K650D-i5 D1.仍旧安装双系统,WIndows7+Fedora20.磁盘分区是这样的: 第一主分区 /boot ext4 20G 第二主分区 / ext4 70G 第 ...
- macbook中出现2003 - Can't connect to MySQL server on '127.0.0.1' (61 "Connection refused") 如何解决
第一步 关闭mysql服务: 苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务(点击stop mysql server) 如果这种方法没有成功: 可以使用命令行 ...
- LVS 负载均衡 三种工作模式 十种调度算法
原文链接:https://blog.csdn.net/weixin_40470303/article/details/80541639 一.LVS简介 LVS(Linux Virtual Server ...
- 使用Markdown编辑总结
Markdown是轻量级标记语言,它允许人们使用易读易写的纯文本格式编写文档. Markdown可以导出HTML .Word.图像.PDF.Epub 等多种格式的文档. 后缀为.md或者.markdo ...
- 在操作Git Bash时出现的问题
参考博客:https://blog.csdn.net/weixin_44394753/article/details/91410463 1.问题1 $ git remote add origin gi ...
- 查看手机wifi密码
方法一 手机共享wifi,获得二维码,之后解码获得密码. 二维码解吗工具: https://jiema.wwei.cn/ 方法二 手机扫描wifi共享的二维码后,会有提示信息,其中会显示出密码.
- flex布局取消子元素(img、div等)缩放:
取消子元素(img.div等)缩放: 父元素: display: flex ; 子元素: flex-shrink: 0;
- Hibernate实现limit语句效果
Hibernate hibernate实现limit效果 由于hql语句内无法直接书写limit语法,所以需要通过别的方式来达成这个效果 limit效果一般需要有两个参数:开始位置start和查询数量 ...