我用 tensorflow 实现的 “一个神经聊天模型”

简介: 这个工作尝试重现这个论文的结果 A Neural Conversational Model (aka the Google chatbot). 它使用了循环神经网络(seq2seq 模型)来进行句子预测。它是用 python 和 TensorFlow 开发。

概述

这个工作尝试重现这个论文的结果 A Neural Conversational Model (aka the Google chatbot).
它使用了循环神经网络(seq2seq 模型)来进行句子预测。它是用 python 和 TensorFlow 开发。

程序的加载主体部分是参考 Torch 的  neuralconvo from macournoyer.

现在, DeepQA 支持一下对话语料:

  • Cornell Movie Dialogs corpus (default). Already included when cloning the repository.

  • OpenSubtitles (thanks to Eschnou). Much bigger corpus (but also noisier). To use it, followthose instructions and use the flag --corpus opensubs.

  • Supreme Court Conversation Data (thanks to julien-c). Available using --corpus scotus. See the instructions for installation.

  • Ubuntu Dialogue Corpus (thanks to julien-c). Available using --corpus ubuntu. See theinstructions for installation.

  • Your own data (thanks to julien-c) by using a simple custom conversation format (See herefor more info).

To speedup the training, it's also possible to use pre-trained word embeddings (thanks toEschnou). More info here.

安装

这个程序需要一下依赖 (easy to install using pip: pip3 install -r requirements.txt):

  • python 3.5

  • tensorflow (tested with v1.0)

  • numpy

  • CUDA (for using GPU)

  • nltk (natural language toolkit for tokenized the sentences)

  • tqdm (for the nice progression bars)

你可能需要下载附带的数据让 nltk 正常工作。

python3 -m nltk.downloader punkt

Cornell 数据集已经包括了。其他的数据集查看 readme 文件到他们所在的文件夹。  (在  data/).

网站接口需要一些附加的包:

  • django (tested with 1.10)

  • channels

  • Redis (see here)

  • asgi_redis (at least 1.0)

Docker 安装也是支持的,更多详细的教程参考  here.

运行

聊天机器人

训练这个模型,直接运行 main.py 。一旦训练完成,你可以测试结果用 main.py --test
(结果生成在  'save/model/samples_predictions.txt') 或者用 main.py --test interactive (更有趣).

Here are some flags which could be useful. For more help and options, use python main.py -h:

  • --modelTag: allow to give a name to the current model to differentiate between them when testing/training.

  • --keepAll: use this flag when training if when testing, you want to see the predictions at different steps (it can be interesting to see the program changes its name and age as the training progress). Warning: It can quickly take a lot of storage space if you don't increase the --saveEvery option.

  • --filterVocab 20 or --vocabularySize 30000: Limit the vocabulary size to and optimize the performances and memory usage. Replace the words used less than 20 times by thetoken and set a maximum vocabulary size.

  • --verbose: when testing, will print the sentences as they are computed.

  • --playDataset: show some dialogue samples from the dataset (can be use conjointly with --createDataset if this is the only action you want to perform).

To visualize the computational graph and the cost with TensorBoard, just run tensorboard --logdir save/.

默认的这个网络架构是一个标准的 encoder/decoder 有两个 LSTM layers (隐藏层大小 256) ,然后 vocabulary 的 embedding size 是  32. 这个网络用 ADAM 训练。最大的句子长度设置为 10 个单词,但是可以增加。

结果

当然,这个网络并不会很擅长聊天:

Q: Hi
A: Hi.

Q: What is your name ?
A: Laura.

Q: What does that mean ?
A: I dunno.

Q: How old are you ?
A: thirty-five.

Q: Will Google hire me ?
A: No.

Q: Tell me the alphabet
A: Fuck you.

Q: That's not nice
A: Yeah.

这里有一些情况它并不能正确回答:

Q: Who is Laura ?
A: My brother.

Q: Say 'goodbye'
A: Alright.

Q: What is cooking ?
A: A channel.

Q: Can you say no ?
A: No.

Q: Two plus two
A: Manny...

预训练模型

项目截图

我用 tensorflow 实现的 一个神经聊天模型

chatbot_miniature.png

实测截图

我用 tensorflow 实现的 一个神经聊天模型

Screenshot from 2017-09-05 14-47-52.png

一步一步教程

1. 下载这个项目:
https://github.com/Conchylicultor/DeepQA
2. 下载训练好的模型:
https://drive.google.com/file/d/0Bw-phsNSkq23OXRFTkNqN0JGUU0/view
(如果网址不能打开的话,今晚我会上传到百度网盘,分享到:http://www.tensorflownews.com/
3. 解压之后放在 项目 save 目录下,如图所示

我用 tensorflow 实现的 一个神经聊天模型

Screenshot from 2017-09-05 14-52-13.png

4. 复制 save/model-pretrainedv2/dataset-cornell-old-lenght10-filter0-vocabSize0.pkl 这个文件到  data/samples/,如图所示:

我用 tensorflow 实现的 一个神经聊天模型

Screenshot from 2017-09-05 14-55-00.png

5. 在项目目录执行一下命令:

python3 main.py --modelTag pretrainedv2 --test interactive

程序读取了预训练的模型之后,如图:

我用 tensorflow 实现的 一个神经聊天模型

Screenshot from 2017-09-05 14-57-14.png



本文作者:AI研习社
本文转自雷锋网禁止二次转载, 原文链接
目录
相关文章
|
3月前
|
机器学习/深度学习 算法 TensorFlow
文本分类识别Python+卷积神经网络算法+TensorFlow模型训练+Django可视化界面
文本分类识别Python+卷积神经网络算法+TensorFlow模型训练+Django可视化界面
63 0
文本分类识别Python+卷积神经网络算法+TensorFlow模型训练+Django可视化界面
|
3月前
|
机器学习/深度学习 监控 Python
tensorflow2.x多层感知机模型参数量和计算量的统计
tensorflow2.x多层感知机模型参数量和计算量的统计
|
6月前
|
TensorFlow 算法框架/工具
【tensorflow】TF1.x保存与读取.pb模型写法介绍
由于TF里面的概念比较接地气,所以用tf1.x保存.pb模型时总是怕有什么操作漏掉了,会造成保存的模型是缺少变量数据或者没有保存图,所以先明确一下:用TF1.x保存模型时只需要保存模型的输入输出的变量(多输入就保存多个),不需要保存中间的变量;用TF1.x加载模型时只需要加载保存的模型,然后读一下输入输出变量(多输入就读多个),不需要初始化(反而会重置掉变量的值)。
|
6月前
|
机器学习/深度学习 TensorFlow 算法框架/工具
【tensorflow】连续输入的线性回归模型训练代码
  get_data函数用于生成随机的训练和验证数据集。首先使用np.random.rand生成一个形状为(10000, 10)的随机数据集,来模拟10维的连续输入,然后使用StandardScaler对数据进行标准化。再生成一个(10000,1)的target,表示最终拟合的目标分数。最后使用train_test_split函数将数据集划分为训练集和验证集。
|
6月前
|
机器学习/深度学习 算法 TensorFlow
树叶识别系统python+Django网页界面+TensorFlow+算法模型+数据集+图像识别分类
树叶识别系统python+Django网页界面+TensorFlow+算法模型+数据集+图像识别分类
133 1
|
6月前
|
机器学习/深度学习 移动开发 算法
动物识别系统python+Django网页界面+TensorFlow算法模型+数据集训练
动物识别系统python+Django网页界面+TensorFlow算法模型+数据集训练
92 0
动物识别系统python+Django网页界面+TensorFlow算法模型+数据集训练
|
6月前
|
机器学习/深度学习 算法 TensorFlow
交通标志识别系统python+TensorFlow+算法模型+Django网页+数据集
交通标志识别系统python+TensorFlow+算法模型+Django网页+数据集
62 0
|
3月前
|
机器学习/深度学习 搜索推荐 算法
推荐系统离线评估方法和评估指标,以及在推荐服务器内部实现A/B测试和解决A/B测试资源紧张的方法。还介绍了如何在TensorFlow中进行模型离线评估实践。
推荐系统离线评估方法和评估指标,以及在推荐服务器内部实现A/B测试和解决A/B测试资源紧张的方法。还介绍了如何在TensorFlow中进行模型离线评估实践。
194 0
|
8天前
|
机器学习/深度学习 TensorFlow 调度
优化TensorFlow模型:超参数调整与训练技巧
【4月更文挑战第17天】本文探讨了如何优化TensorFlow模型的性能,重点介绍了超参数调整和训练技巧。超参数如学习率、批量大小和层数对模型性能至关重要。文章提到了三种超参数调整策略:网格搜索、随机搜索和贝叶斯优化。此外,还分享了训练技巧,包括学习率调度、早停、数据增强和正则化,这些都有助于防止过拟合并提高模型泛化能力。结合这些方法,可构建更高效、健壮的深度学习模型。
|
1月前
|
机器学习/深度学习 TensorFlow 算法框架/工具
OpenCV读取tensorflow 2.X模型的方法:将SavedModel转为frozen graph
【2月更文挑战第22天】本文介绍基于Python的tensorflow库,将tensorflow与keras训练好的SavedModel格式神经网络模型转换为frozen graph格式,从而可以用OpenCV库在C++等其他语言中将其打开的方法~
OpenCV读取tensorflow 2.X模型的方法:将SavedModel转为frozen graph

热门文章

最新文章