Pytext上手——Intent-Slot 模型实战

栏目: Python · 发布时间: 7年前

内容简介:对话系统里,首先要对用户的输入进行领域、意图识别和槽抽取。深度学习发展以后,意图识别的方法多使用深度学习的算法,使用CNN对意图进行多分类,领域分类和意图分类较为类似。而槽的预测可以看成是标签序列预测问题。例如句子“我想听周杰伦的菊花台”,标签可以定义为“O O O B-singer M-singer E-singer O B-song M-song E-song”。标签序列预测多使用CRF,RNN,LSTM,LSTM+crf的模型。 链接:槽位填充可以理解为一个序列标注的问题,我们训练范例{(x^((n

对话系统里,首先要对用户的输入进行领域、意图识别和槽抽取。深度学习发展以后,意图识别的方法多使用深度学习的算法,使用CNN对意图进行多分类,领域分类和意图分类较为类似。而槽的预测可以看成是标签序列预测问题。例如句子“我想听周杰伦的菊花台”,标签可以定义为“O O O B-singer M-singer E-singer O B-song M-song E-song”。标签序列预测多使用CRF,RNN,LSTM,LSTM+crf的模型。 链接: www.zhihu.com/question/22…

2 槽位填充

槽位填充可以理解为一个序列标注的问题,我们训练范例{(x^((n)),y^((n)) ):n=1,……,N},然后我们想要识别学到一个函数f∶x→y,这个函数能够匹配输入序列x和相应的标签序列y。在槽位填充中,输入序列和标签序列长度相同,因此排列是准确的。

Pytext上手——Intent-Slot 模型实战

3 数据集ATIS

ATIS数据集包含4978训练数据和893个测试数据,文本内容为客服对话,意图一共有26类。查询话语中的每个标记与填充IOB标签的插槽对齐,也就是上面图片中Sentence和Slots都是一一对齐的。

4 Pytext实战

本部分内容主要参考官方的文档 Train Intent-Slot model on ATIS Dataset ,有些地方稍微出入。

4.1 安装

目前Pytext只支持 Linux 和Mac系统,在命令行输入下面语句安装:

pip install pytext-nlp
复制代码

4.2 文件准备

Pytext上手——Intent-Slot 模型实战
pytext github.com/facebookres… 数据集 www.kaggle.com/siddhadev/a…

4.3 数据预处理

python3 demo/atis_joint_model/data_processor.py --download-folder atis/ --output-directory demo/atis_joint_model/
复制代码
Pytext上手——Intent-Slot 模型实战

4.4 模型训练

pytext train < demo/atis_joint_model/atis_joint_config.json
复制代码

在没有使用GPU的情况下,训练需要30分钟左右

Pytext上手——Intent-Slot 模型实战

模型训练完毕时,我们通过 atis_joint_config.json 看到,结果文件和模型保存到 tmp目录下

Pytext上手——Intent-Slot 模型实战

4.6 模型导出

保存PyTorch模型时,简单的使用pickle进行序列化。这意味着简单的代码更改(例如,单词嵌入更新)可能导致与已部署模型的向后不兼容。为了解决此问题,可以使用内置的ONNX集成将模型导出为Caffe2格式。无论PyText或开发代码中的更改如何,导出的Caffe2模型都具有相同的行为。

在命令行中分别输入下面两行语句

CONFIG=demo/atis_joint_model/atis_joint_config.json
复制代码
pytext export --output-path exported_model.c2 < "$CONFIG"
复制代码

4.5 模型评估

我们可以使用 pytext test 来测试模型在测试集上的表现

pytext test < "$CONFIG"
复制代码
Pytext上手——Intent-Slot 模型实战

4.6 模型应用

我们可以将训练的模型部署成一个web应用,新建文件 flask_app.py

import sys
import flask
import pytext

config_file = sys.argv[1]
model_file = sys.argv[2]

config = pytext.load_config(config_file)
predictor = pytext.create_predictor(config, model_file)

app = flask.Flask(__name__)

@app.route('/get_flight_info', methods=['GET', 'POST'])
def get_flight_info():
    text = flask.request.data.decode()

    # Pass the inputs to PyText's prediction API
    result = predictor({"raw_text": text})

    # Results is a list of output blob names and their scores.
    # The blob names are different for joint models vs doc models
    # Since this tutorial is for both, let's check which one we should look at.
    doc_label_scores_prefix = (
        'scores:' if any(r.startswith('scores:') for r in result)
        else 'doc_scores:'
    )

    # For now let's just output the top document label!
    best_doc_label = max(
        (label for label in result if label.startswith(doc_label_scores_prefix)),
        key=lambda label: result[label][0],
    # Strip the doc label prefix here
    )[len(doc_label_scores_prefix):]

    return flask.jsonify({"question": f"Are you asking about {best_doc_label}?"})

app.run(host='0.0.0.0', port='8080', debug=True)
复制代码

执行

python flask_app.py "$CONFIG" exported_model.c2
复制代码

然后打开另一个Terminal,我们测试下服务: 测试1

curl http://localhost:8080/get_flight_info -H "Content-Type: text/plain" -d  "I am looking for flights from San Francisco to Minneapolis"

{
  "question": "Are you asking about flight?"
}

复制代码

测试2

curl http://localhost:8080/get_flight_info -H "Content-Type: text/plain" -d  "How much does a trip to NY cost?"

{
  "question": "Are you asking about airfare?"
}

复制代码

测试3

curl http://localhost:8080/get_flight_info -H "Content-Type: text/plain" -d  "Which airport should I go to?"

{
  "question": "Are you asking about airport?"
}
复制代码

我们可以看到,模型将3次的意图都识别到了。

Pytext上手——Intent-Slot 模型实战

以上所述就是小编给大家介绍的《Pytext上手——Intent-Slot 模型实战》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Using Google App Engine

Using Google App Engine

Charles Severance / O'Reilly Media / 2009-5-23 / USD 29.99

With this book, you can build exciting, scalable web applications quickly and confidently, using Google App Engine - even if you have little or no experience in programming or web development. App Eng......一起来看看 《Using Google App Engine》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器