TF-NNLM-TK是用Python3编写的工具包,用于使用TensorFlow进行神经网络语言建模。它包括RNN和LSTMS等基本模型以及更高级的模型。它提供了预处理数据,训练模型并评估它们的功能。该工具包在Apache 2许可证下开源。
目前,支持以下模型:
首先安装Python和TensorFlow。该代码用Python 3和TensorFlow 1.8测试。
在第一个示例中,我们需要下载工具包和一些培训数据。我们将使用Tomas Mikolov教程中提供的PTB数据集。为此,您可以在命令行中运行以下代码:
git clone git clone https://github.com/uds-lsv/TF-NNLM-TK.git
cd TF-NNLM-TK
wget http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz
tar -xzf simple-examples.tgz然后,要训练和评估第一个简单的神经语言模型,只需运行以下代码
python train_basic_rnn_models.py --save_dir=small_lstm --model=lstm --train_file=simple-examples/data/ptb.train.txt --test_file=simple-examples/data/ptb.test.txt
python test.py --model_file=small_lstm/model.ckpt --test_file=simple-examples/data/ptb.test.txt在GTX 1050TI GPU上,训练大约需要20分钟。
培训脚本已经调用数据处理代码。如果使用默认数据,则可以跳过本节。
该工具包包括一个数据处理器,该数据处理器读取文本文件并从中创建两个(numpy)数组,该数组存储了输入单词(历史记录)和目标词(预测)的批处理。该代码还提供了一些有用的工具,例如功能,以创建和保存词汇,创建计数或将测试文件中的OOV单词映射到给定的未知令牌中。
代码的这一部分没有主要功能。相反,它是在训练脚本的Python代码中直接调用的。例如,您可以打电话给Python
DataProcessor ( train_file , batch_size , seq_length , True , '<unk>' , history_size = history_size )此代码段将采用Train_file,进行上述转换,并使用给定参数从中创建批处理。
TF-NNLM-TK为以下神经语言模型提供了培训代码:
这些是常见和使用的。特别是,该工具包通过投影和GRU实现Vanilla-RNN,LSTM,LSTM。可以使用脚本train_basic_rnn_models.py训练这些模型(请参见下面的示例)。
这些模型的使用不仅仅是历史中的n个单词,而不是最后一个单词。该实现提供了三个模型:依赖单词的SRNN(WD-SRNN),独立的SRNN(WI-SRNN)和忘记因子SRNN(FF-SRNN)。有关这些模型的更多信息,请参见此处。这些模型可以使用脚本train_srnn.py培训
这些模型使用两个独立的本地和全球状态分别学习短期和长期依赖性。反向传播的分段TF实施使该模型在使用香草-RNN模型的局部状态下急剧遭受了消失的梯度的影响,因此后者被替换(暂时由GRU)。有关此模型的更多信息,请参见此处。可以使用脚本train_lsrc.py训练此模型
这些训练脚本中的每一个(train_basic_rnn_models.py,train_srnn.py and train_lsrc.py)都包含大量参数,每个参数都附加了描述。例如,在您的命令行上获得此说明运行:
python train_basic_rnn_models.py --help所有模型的默认参数都试图匹配Tensorflow PTB-LM配方中报告的小配置:
| config | 时代 | 火车 | 有效的 | 测试 |
|---|---|---|---|---|
| 小的 | 13 | 37.99 | 121.39 | 115.91 |
| 中等的 | 39 | 48.45 | 86.16 | 82.07 |
| 大的 | 55 | 37.87 | 82.62 | 78.29 |
要使用LSTM模型(实际上是由于Xavier初始化而导致的数字),请运行(调整到设置数据的路径):
python train_basic_rnn_models.py --save_dir=small_lstm --model=lstm --train_file=path/to/data/train.txt --test_file=path/to/data/test.txt此调用将使用Tensorflow配方中使用的完全相同的配置在PTB数据上训练LSTM模型。如果要使用媒体配置运行模型,则只需要将参数设置为中等配置中指定的值:
python train_basic_rnn_models.py --init_scale=0.05 --seq_length=35 --embed_size=650 --hidden_size=650 --max_epoch=6 --num_epochs=39 --decay_rate=0.8 --batch_size=20 --input_keep_prob=0.5 --output_keep_prob=0.5 --model=lstm --save_dir=medium_lstm --train_file=path/to/data/train.txt --test_file=path/to/data/test.txt同样的想法适用于更高级的模型,只是您需要调用相应的培训脚本,并且可能需要稍微调整参数。例如,尝试:
python train_srnn.py --model=wi-srnn --input_keep_prob=0.6 --save_dir=wisrnn_small_5gram --train_file=path/to/data/train.txt --test_file=path/to/data/test.txt 用默认配置训练WISRNN模型,除了单词嵌入掉落,该单词设置为0.4(1-0.6)。这应该导致示例数据集的性能约为109.5。
同样,使用相应的脚本训练LSRC模型:
python train_lsrc.py --save_dir=lsrc_small --train_file=path/to/data/train.txt --test_file=path/to/data/test.txt 该脚本还允许通过设置相应的参数来修改训练。使用 - 支持以获取有关它们的更多信息。
所有模型的测试脚本都是相同的。您只需要指定要评估的模型的路径以及测试文件的路径。为了评估我们上面训练的小型LSTM模型,我们只需要运行以下命令:
python test.py --model_file=small_lstm/model.ckpt --test_file=path/to/data/test.txt 该脚本还提供了其他一些参数,以控制您在非常大的语料库上测试的速度。
该工具包最初是由Youssef Oualil在Saarland University的LSV期间开发的。目前,迈克尔·A·赫德里奇(Michael A.这项工作由SFB 1102部分资助。
此代码在Apache 2.0下许可。该代码的一部分基于tensorflow PTB-LM食谱,该配方是Apache许可证的许可,由Tensorflow作者版本2.0版。请参阅许可证文件以获取详细信息。
如果您将此工具包作为出版物的一部分,请考虑引用我们:
@inproceedings{oualil-singh-greenberg-klakow:EMNLP2016,
author = {Oualil, Youssef and Singh, Mittul and Greenberg, Clayton and Klakow, Dietrich},
title = {Long-Short Range Context Neural Networks for Language Modeling},
booktitle = {{EMNLP} 2016, Proceedings of the 2016 Conference on Empirical Methods in Natural Language Processing},
day = {3},
month = {November},
year = {2016},
address = {Austin, Texas},
publisher = {Association for Computational Linguistics},
pages = {1473--1481},
url = {http://aclweb.org/anthology/D16-1154.pdf},
poster = {http://coli.uni-saarland.de/~claytong/posters/EMNLP16_Poster.pdf}
}
或者
@inproceedings{oualil-greenberg-singh-klakow:2016:IS,
author = {Youssef Oualil and Clayton Greenberg and Mittul Singh and Dietrich Klakow},
title = {Sequential Recurrent Neural Networks for Language Modeling},
day = {12},
month = {September},
year = 2016,
address = {San Francisco, California, USA},
booktitle = {{INTERSPEECH} 2016, Proceedings of the 17th Annual Conference of the International Speech Communication Association},
doi = {10.21437/Interspeech.2016-422},
url = {http://www.isca-speech.org/archive/Interspeech_2016/pdfs/0422.PDF},
pages = {3509--3513},
publisher = {{ISCA}}
}