我用 tensorflow 实现的“一个神经聊天模型”:一个基于深度学习的聊天机器人...

概述

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

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

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

To speedup the training, it's also possible to use pre-trained word embeddings (thanks to Eschnou). 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 <name>: 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 the <unknown> token 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...

预训练模型

项目截图:

chatbot_miniature.png

实测截图:

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 目录下
如图所示

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

4.复制 save/model-pretrainedv2/dataset-cornell-old-lenght10-filter0-vocabSize0.pkl 这个文件到 data/samples/

如图所示:

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

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

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

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

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

聊天机器人资源合集

项目,语聊,论文,教程
https://github.com/fendouai/Awesome-Chatbot

更多教程:

http://www.tensorflownews.com/

DeepQA

https://github.com/Conchylicultor/DeepQA

备注:为了更加容易了解这个项目,说明部分翻译了项目的部分 readme ,主要是介绍使用预处理数据来运行这个项目。

转载于:https://www.cnblogs.com/tensorflownews/p/7479623.html

# 说明 该库是对目前市面上已有的开源中文聊天语料的搜集和系统化整理工作 该库搜集了包含 - chatterbot - 豆瓣多轮 - PTT八卦语料 - 青云语料 - 电视剧对白语料 - 贴吧论坛回帖语料 - 微博语料 - 小黄鸡语料 共8个公开闲聊常用语料和短信,白鹭时代问答等语料。 并对8个常见语料的数据进行了统一化规整和处理,达到直接可以粗略使用的目的。 **使用该项目,即可对所有的聊天语料进行一次性的处理和统一下载,不需要到处自己去搜集下载和分别处理各种不同的格式。* # 环境 python3 # 处理过程 将各个来源的语料按照其原格式进行提取,提取后进行繁体字转换,然后统一变成一轮一轮的对话。 # 使用方法 将解压后的raw_chat_corpus文件夹放到当前目录下 目录结构为 ``` raw_chat_corpus -- language -- process_pipelines -- raw_chat_corpus ---- chatterbot-1k ---- douban-multiturn-100w ---- .... -- main.py -- ... ``` 执行命令即可 ```bash python main.py ``` 或者 ```bash python3 main.py ``` # 生成结果 每个来源的语料分别生成一个独立的*.tsv文件,都放在新生成的clean_chat_corpus文件夹下。 生成结果格式为 tsv格式,每行是一个样本,先是query,再是answer ``` query \t answer ``` # 结果的使用 这个就根据每个人不同的情况自主使用即可 个人对于聊天机器人方向实践也不是很多,以下一篇之前写的知乎专栏供参考 **《从产品完整性的角度浅谈chatbot》** 文章粗略讲解了如下一些方面,介绍了聊天机器人在实际产品化过程中可能遇到的问题和解决办法。 1. chatbot自身人格的设置 1. 产品上线需要考虑的敏感词处理 1. 文本检索模型的使用 1. 文本生成模型的使用 1. 回答打分机制 1. 万能回答的使用策略 1. 多媒体消息的处理 1. 产品模型部署的问题 # 版权说明 本项目为非商业项目,为纯搜集和汇总资料,如有侵权,请在issue下留言。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值