语言模型
学习目标
- 学习语言模型,以及如何训练一个语言模型
- 学习torchtext的基本使用方法
- 构建 vocabulary
- word to inde 和 index to word
- 学习torch.nn的一些基本模型
- Linear
- RNN
- LSTM
- GRU
- RNN的训练技巧
- Gradient Clipping
- 如何保存和读取模型
我们会使用 torchtext 来创建vocabulary, 然后把数据读成batch的格式。请大家自行阅读README来学习torchtext。
In [1]:
import torchtext from torchtext.vocab import Vectors import torch import numpy as np import random USE_CUDA = torch.cuda.is_available() # 为了保证实验结果可以复现,我们经常会把各种random seed固定在某一个值 random.seed(53113) np.random.seed(53113) torch.manual_seed(53113) if USE_CUDA: torch.cuda.manual_seed(53113) BATCH_SIZE = 32 EMBEDDING_SIZE = 650 MAX_VOCAB_SIZE = 50000
- 我们会继续使用上次的text8作为我们的训练,验证和测试数据
- TorchText的一个重要概念是
Field
,它决定了你的数据会如何被处理。我们使用TEXT
这个field来处理文本数据。我们的TEXT
field有lower=True
这个参数,所以所有的单词都会被lowercase。 - torchtext提供了LanguageModelingDataset这个class来帮助我们处理语言模型数据集。
build_vocab
可以根据我们提供的训练数据集来创建最高频单词的单词表,max_size
帮助我们限定单词总量。- BPTTIterator可以连续地得到连贯的句子,BPTT的全程是back propagation through time。
In [2]:
TEXT = torchtext.data.Field(lower=True) train, val, test = torchtext.datasets.LanguageModelingDataset.splits(path=".", train="text8.train.txt", validation="text8.dev.txt", test="text8.test.txt", text_field=TEXT) TEXT.build_vocab(train, max_size=MAX_VOCAB_SIZE) print("vocabulary size: {}".format(len(TEXT.vocab))) VOCAB_SIZE = len(TEXT.vocab) train_iter, val_iter, test_iter = torchtext.data.BPTTIterator.splits( (train, val, test), batch_size=BATCH_SIZE, device=-1, bptt_len=32, repeat=False, shuffle=True)
The `device` argument should be set by using `torch.device` or passing a string as an argument. This behavior will be deprecated soon and currently defaults to cpu. The `device` argument should be set by using `torch.device` or passing a string as an argument. This behavior will be deprecated soon and currently defaults to cpu. The `device` argument should be set by using `torch.device` or passing a string as an argument. This behavior will be deprecated soon and currently defaults to cpu.
vocabulary size: 50002
- 为什么我们的单词表有50002个单词而不是50000呢?因为TorchText给我们增加了两个特殊的token,
<unk>
表示未知的单词,<pad>
表示padding。 - 模型的输入是一串文字,模型的输出也是一串文字,他们之间相差一个位置,因为语言模型的目标是根据之前的单词预测下一个单词。
In [5]:
it = iter(train_iter) batch = next(it) print(" ".join([TEXT.vocab.itos[i] for i in batch.text[:,1].data])) print(" ".join([TEXT.vocab.itos[i] for i in b