pytorch attentive lm
1.0.0
此存储库是针对语言建模任务的细心RNN模型的实现。
语言建模均在Penntreebank和Wikitext-02数据集上完成。解析文件,以使每个培训示例由一个句子组成,从语料库中,最大批次长度为35。剪辑更长的句子。这样做是为了管理注意力并仅在句子中注意单词(如果在时间段t之前timestep t之前)。
A-RNN-LM(基于注意力的语言建模神经网络)最初是在与基于注意力的语言模型(Hongyuan Mei等人,2016年,Link)和细心语言模型(Salton et al.2017,Link)中进行连贯的对话。
该模型包括在编码器RNN层的先前隐藏状态上运行传统的注意机制,以编码上下文向量,然后将其与最后一个编码的隐藏状态结合使用,以预测序列中的下一个单词。
依赖性:
python=3.7torch>=1.0.0nltkmatplotlibtensorboardX安装所有depepecties并运行python main.py
数据集将自动下载和预处理。
运行的多个选项可能是运行python main.py --help列表。
usage: main.py [-h] [--batch-size N] [--epochs N] [--lr LR] [--patience P]
[--seed S] [--log-interval N] [--dataset [{wiki-02,ptb}]]
[--embedding-size N] [--n-layers N] [--hidden-size N]
[--positioning-embedding N] [--input-dropout D]
[--rnn-dropout D] [--decoder-dropout D] [--clip N]
[--optim [{sgd,adam,asgd}]] [--salton-lr-schedule]
[--early-stopping-patience P] [--attention]
[--no-positional-attention] [--tie-weights]
[--file-name FILE_NAME] [--parallel]
PyTorch Attentive RNN Language Modeling
optional arguments:
-h, --help show this help message and exit
--batch-size N input batch size for training (default: 64)
--epochs N number of epochs to train (default: 40)
--lr LR learning rate (default: 30.0)
--patience P patience for lr decrease (default: 5)
--seed S random seed (default: 123)
--log-interval N how many batches to wait before logging training
status (default 10)
--dataset [{wiki-02,ptb}]
Select which dataset (default: ptb)
--embedding-size N embedding size for embedding layer (default: 20)
--n-layers N layer size for RNN encoder (default: 1)
--hidden-size N hidden size for RNN encoder (default: 20)
--positioning-embedding N
hidden size for positioning generator (default: 20)
--input-dropout D input dropout (default: 0.5)
--rnn-dropout D rnn dropout (default: 0.0)
--decoder-dropout D decoder dropout (default: 0.5)
--clip N value at which to clip the norm of gradients (default:
0.25)
--optim [{sgd,adam,asgd}]
Select which optimizer (default: sgd)
--salton-lr-schedule Enables same training schedule as Salton et al. 2017
(default: False)
--early-stopping-patience P
early stopping patience (default: 25)
--attention Enable standard attention (default: False)
--no-positional-attention
Disable positional attention (default: False)
--tie-weights Tie embedding and decoder weights (default: False)
--file-name FILE_NAME
Specific filename to save under (default: uses params
to generate)
--parallel Enable using GPUs in parallel (default: False)
| 模型 | 参数数 | 验证困惑 | 测试困惑 |
|---|---|---|---|
| LSTM基线(Merity等,2017) | 786万 | 66.77 | 64.96 |
| 细心的LM(Salton等,2017) | 7.06m | 79.09 | 76.56 |
| 位置专心LM | 6.9m | 72.69 | 70.92 |
| 模型 | 参数数 | 验证困惑 | 测试困惑 |
|---|---|---|---|
| LSTM基线(Merity等,2017) | 786万 | 72.43 | 68.50 |
| 细心的LM(Salton等,2017) | 7.06m | 78.43 | 74.37 |
| 位置专心LM | 6.9m | 74.39 | 70.73 |
您可以通过简单地运行来重新运行所有生成上表的模型:
python test.py
但是,请注意,其中一些型号需要超过8小时的时间来收敛于单个1080 GPU,因此实验的总运行时间可能约为2天。
默认情况下,多GPU支持将被禁用,因为它被证明对结果产生负面影响。最重要的是,由于批次在实践中很小,因此实际上并不多,因为花了很多时间将张量发送到各自的GPU。
此处显示了两个注意分布的并排比较:
x轴中的单词是每个时间步骤的输入,y轴中的单词是目标。两种模型均在Wikitext-02数据集上进行训练,直到收敛为止。