Pytorch
torch.nn.utils.rnn:
pack_padded_sequence() pad_packed_sequence()
Notice:
- The padded embedding metrix must be sorted by the ground length of each sentence.
- The parameter "batch_first=True" controls the first demension of the embedding metrix is the batch_size (\(B\times T \times W_{emb}\)). Otherwise, the first demension is the length of the longest sentence (\(T\times B \times W_{emb}\))
- The two functions will inflence the padding word, especially on bidirectional RNN (the backward rnn will go through some padding words first and the forward rnn will go through some padding words last).
Examplt
a = torch.FloatTensor([[[2,3,4], [2, 3,1]], [[2,3,4], [2, 3,1]], [[2,4,5], [0, 0, 0]]])
packed_x = pack_padded_sequence(a, [2, 2, 1], batch_first=True)
# a[0,0,:] = torch.FloatTensor([0,0,0])
h0 = Variable(torch.randn(1, 3, 4))
lstm = nn.LSTM(3, 4,num_layers=1,batch_first=True,bidirectional=True)
rnn_packed, (h_last, c_last) = lstm(packed_x)
rnn_out, length = pad_packed_sequence(rnn_packed, batch_first=True)
Nan
Mask
When using mask on rnn outputs with \(-\infty\), it will cause the grad to be nan when back-propogating. But it's right when appling to attention. The \(softmax\) operation does not cause the nan problem.
Pytorch的更多相关文章
- Ubutnu16.04安装pytorch
1.下载Anaconda3 首先需要去Anaconda官网下载最新版本Anaconda3(https://www.continuum.io/downloads),我下载是是带有python3.6的An ...
- 解决运行pytorch程序多线程问题
当我使用pycharm运行 (https://github.com/Joyce94/cnn-text-classification-pytorch ) pytorch程序的时候,在Linux服务器 ...
- 基于pytorch实现word2vec
一.介绍 word2vec是Google于2013年推出的开源的获取词向量word2vec的工具包.它包括了一组用于word embedding的模型,这些模型通常都是用浅层(两层)神经网络训练词向量 ...
- 基于pytorch的CNN、LSTM神经网络模型调参小结
(Demo) 这是最近两个月来的一个小总结,实现的demo已经上传github,里面包含了CNN.LSTM.BiLSTM.GRU以及CNN与LSTM.BiLSTM的结合还有多层多通道CNN.LSTM. ...
- pytorch实现VAE
一.VAE的具体结构 二.VAE的pytorch实现 1加载并规范化MNIST import相关类: from __future__ import print_function import argp ...
- PyTorch教程之Training a classifier
我们已经了解了如何定义神经网络,计算损失并对网络的权重进行更新. 接下来的问题就是: 一.What about data? 通常处理图像.文本.音频或视频数据时,可以使用标准的python包将数据加载 ...
- PyTorch教程之Neural Networks
我们可以通过torch.nn package构建神经网络. 现在我们已经了解了autograd,nn基于autograd来定义模型并对他们有所区分. 一个 nn.Module模块由如下部分构成:若干层 ...
- PyTorch教程之Autograd
在PyTorch中,autograd是所有神经网络的核心内容,为Tensor所有操作提供自动求导方法. 它是一个按运行方式定义的框架,这意味着backprop是由代码的运行方式定义的. 一.Varia ...
- Linux安装pytorch的具体过程以及其中出现问题的解决办法
1.安装Anaconda 安装步骤参考了官网的说明:https://docs.anaconda.com/anaconda/install/linux.html 具体步骤如下: 首先,在官网下载地址 h ...
- Highway Networks Pytorch
导读 本文讨论了深层神经网络训练困难的原因以及如何使用Highway Networks去解决深层神经网络训练的困难,并且在pytorch上实现了Highway Networks. 一 .Highway ...
随机推荐
- http和https协议的区别
内容摘自:转载注明Wosign 尊重知识产权 一 什么是HTTPS HTTPS全称为 安全超文本传输协议(Secure Hypertext Transfer Protocol)),是一个安全通信通道, ...
- OAuth2
OAuth2: 适合To C的应用场景, 比如我们开发一个app, 可以借用微信/微博用户认证开放接口, 达到免注册登陆, 企业内部系统没有必要引入. OAuth2的步骤较多, 角色也较多, 涉及到a ...
- JavaScript定义类和实例化示例
1.类定义: var UseIScrollDataHelper = { myScroll: null, //iScroll对象 scrollId: 'divscroll',//默认scrollid w ...
- JavaScript传递参数方法
1.SetTimer传递参数 setTimeout(function (obj) { obj.myScroll = new IScroll('#wrapper', { click: true }); ...
- Thinkphp生成的验证码不显示——解决方法
在调用验证码之前加上 ob_clean(); 不显示验证码的代码: public function verify(){ $verify = new \Think\Verify(); $verify-& ...
- mysql8用户管理
查看当前登录用户: 创建用户: create user '用户名'@'主机地址' identified with mysql_native_password by '密码'; 修改密码: alter ...
- Mail.Ru Cup 2018 Round 3 B. Divide Candies
题目链接 分析一下题意可以得到题目要求的是满足下面这个 公式的不同的i,ji,ji,j的方案数; 即(i2+j2)mod   m=0 (n ≤ ...
- 在Windows2008r2 安装.net4.5
WebApi 是比较高的环境下面开发 需要的环境是net4.5 或以上. Windows2008r2 里面没有这个环境必须自己安装.安装net4.5 必须sp1环境以上才能安装. 将Windows20 ...
- 【gitlab】gitlab快速部署教程
gitlab快速部署教程 部署环境 Ubuntu 16.04(亲测可用) 开始部署 安装依赖 sudo apt-get install curl openssh-server ca-certifica ...
- 使用Github时遇到问题的解决方法
记录了一些我在使用Github时遇到问题的解决方法 git中报unable to auto-detect email address 错误的解决办法 问题描述: 执行 git commit -m &q ...