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

1 意图分类任务简介

对话系统里,首先要对用户的输入进行领域、意图识别和槽抽取。深度学习发展以后,意图识别的方法多使用深度学习的算法,使用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。在槽位填充中,输入序列和标签序列长度相同,因此排列是准确的。

表1:ATIS语料样本及其意图和槽位注释

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 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/
复制代码

数据预处理

4.4 模型训练

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

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

模型训练

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

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"
复制代码

评估结果

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次的意图都识别到了。

5 总结

其实,pytext核心部分是针对不同任务的config.json文件,里面定义了模型的结构,输入、输出等等,另外也有网友反映,现在文档对这一部分解释少,对怎么添加模型的介绍也很少,所以只能先摸索下。本篇文章,只是安装官方文档将训练一个模型的流程打通,但是自己希望接下来研究下怎么添加自定义模型和训练中文语料。

我的简书 致Great

猜你喜欢

转载自juejin.im/post/5c1a001df265da61263819ba