使用 tensorflow 生成语言模型初体验

本贴最后更新于 1897 天前,其中的信息可能已经时移世易

背景

语言模型(Language Model, LM): 给出一句话的前 k 个词,来预测第 k+1 个词是什么,给出一个第 k+1 个词可能出现的概率的分布 p(xk+1|x1,x2,...,xk)。

换句话来说,语言模型是一个拟合概率模型,可以给出句子出现的概率。通过学习历史的文本记录,来实现预测后续的字词。

初体验

为了完成这次的小小体验,本文选取了宾州树库 (PTB) 数据集。这个数据集压缩包只有 33M,占用空间小,训练速度快,适合于学习。

安装 tensorflow

tensorflow 不支持 python 3.7,所以本文使用了 python 2.7。

$ sudo pip install --index-url http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com tensorflow

下载 ptb 数据集

$ wget "http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz"

下载代码

  • ptb_word_lm: https://github.com/tensorflow/models/blob/master/tutorials/rnn/ptb/ptb_word_lm.py
  • reader: https://github.com/tensorflow/models/blob/master/tutorials/rnn/ptb/reader.py
  • util: https://github.com/tensorflow/models/blob/master/tutorials/rnn/ptb/util.py
$ wget "https://raw.githubusercontent.com/tensorflow/models/master/tutorials/rnn/ptb/ptb_word_lm.py"
$ wget "https://raw.githubusercontent.com/tensorflow/models/master/tutorials/rnn/ptb/reader.py"
$ wget "https://raw.githubusercontent.com/tensorflow/models/master/tutorials/rnn/ptb/util.py"

运行

$ time python ptb_word_lm.py --data_path=simple-examples/data/ --num_gpus=0

耗时

real    22m10.767s
user    373m22.734s
sys     28m11.109s

运行结果

Epoch: 1 Learning rate: 1.000
0.004 perplexity: 4887.851 speed: 2941 wps
0.104 perplexity: 833.255 speed: 9325 wps
0.204 perplexity: 623.097 speed: 9819 wps
0.304 perplexity: 508.344 speed: 10000 wps
0.404 perplexity: 441.620 speed: 10097 wps
0.504 perplexity: 397.859 speed: 10155 wps
0.604 perplexity: 360.096 speed: 10196 wps
0.703 perplexity: 334.278 speed: 10224 wps
0.803 perplexity: 313.727 speed: 10247 wps
0.903 perplexity: 295.207 speed: 10262 wps
Epoch: 1 Train Perplexity: 281.051
Epoch: 1 Valid Perplexity: 181.208
…………
0.004 perplexity: 67.781 speed: 9579 wps
0.104 perplexity: 52.330 speed: 10272 wps
0.204 perplexity: 56.718 speed: 10292 wps
0.304 perplexity: 54.545 speed: 10293 wps
0.404 perplexity: 53.512 speed: 10291 wps
0.504 perplexity: 52.704 speed: 10300 wps
0.604 perplexity: 50.949 speed: 10305 wps
0.703 perplexity: 50.123 speed: 10305 wps
0.803 perplexity: 49.264 speed: 10313 wps
0.903 perplexity: 47.685 speed: 10322 wps
Epoch: 13 Train Perplexity: 46.632
Epoch: 13 Valid Perplexity: 127.323

perplexity

PPL:根据每个词来估计一句话出现的概率,并用句子长度作 normalize,用来衡量语言模型收敛情况。

PPL 越小,p(wi)则越大,期望的 sentence 出现的概率就越高,模型越好。

WPS

WPS(word per second, 每分钟词数): 训练速度

过程解读

ptb_word_lm.py 中,首先生成数据,

生成数据

  raw_data = reader.ptb_raw_data(FLAGS.data_path)
  train_data, valid_data, test_data, _ = raw_data

可视化

通过 PTBProducer 的 name scope,来准备数据。

imagepng

内部细节图

imagepng

参考

  • Python

    Python 是一种面向对象、直译式电脑编程语言,具有近二十年的发展历史,成熟且稳定。它包含了一组完善而且容易理解的标准库,能够轻松完成很多常见的任务。它的语法简捷和清晰,尽量使用无异义的英语单词,与其它大多数程序设计语言使用大括号不一样,它使用缩进来定义语句块。

    535 引用 • 672 回帖 • 2 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...