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 ...
随机推荐
- 第二节:SSL证书的申请、配置(IIS通用)及跳转Https请求的两种方式
一. 相关概念介绍 1. SSL证书服务 SSL证书服务由"服务商"联合多家国内外数字证书管理和颁发的权威机构.在xx云平台上直接提供的服务器数字证书.您可以在阿里云.腾讯云等平台 ...
- uCosII中的任务
任务基本概念 任务是一个接受操作系统管理的独立运行单元,在uCosII中类似与普通平台上的main()函数,需要自己来保护其因调用或中断二产生的断点,所以需要一个自己的私有堆栈,即任务堆栈: 任务有两 ...
- XGBoost参数调优完全指南
简介 如果你的预测模型表现得有些不尽如人意,那就用XGBoost吧.XGBoost算法现在已经成为很多数据工程师的重要武器.它是一种十分精致的算法,可以处理各种不规则的数据.构造一个使用XGBoost ...
- C#中Dictionary的介绍
关键字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionar ...
- 【opencv实践】边缘检测
边缘检测: 一.canny算子 Canny边缘检测根据对信噪比与定位乘积进行测度,得到最优化逼近算子,也就是Canny算子.类似与 LoG 边缘检测方法,也属于先平滑后求导数的方法. 二.canny算 ...
- C# Datetime时间指定时区
string start_time_str = "2018-03-21 06:00:00"; DateTime.Parse(start_time_str) // :: 格林威治时间 ...
- Anniversary party POJ - 2342 (树形DP)
题目链接: POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...
- 基于Python的Webservice开发(二)-如何用Spyne开发Webservice
一.功能需求 本次案例是开发一个Item的新建的WebService.IN&OUT的类型JsonDocument. 通过传入相关的参数创建Item,且相关的参数可以被缺省. 二.实现代码 引入 ...
- Mongodb 相关链接
http://www.cnblogs.com/lanceyan/tag/mongodb/
- 2018-2019-2-20175225 实验一 《Java开发环境的熟悉》实验报告
2018-2019-2-20175225 实验一 <Java开发环境的熟悉>实验报告 一.实验内容及知识点 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用IDEA编辑.编 ...