美文网首页rasa
rasa对话系统踩坑记(三)

rasa对话系统踩坑记(三)

作者: colin_gao | 来源:发表于2018-11-16 10:23 被阅读0次

rasa对话系统踩坑记(二)中我自定义过两个component组件。也好久没更新采坑系列了,随着项目的进展迭代最近又新增了两个component组件,所以更新下系列文章。

rasa_nlu_gao自定义component

entity_edit_intent

entity_edit_intent这个组件比较简单,写它的目的是因为碰到了这样一个问题。就是在nlu过程中,slot filling倒是很准确,但是intent一直识别不出来都是None.所以起初第一个想到的方案就是根据entity来修改intent。代码大概的思路就是在最后,在entity识别的情况下,如果intent没有识别出来就强制修改意图。

    defaults = {
        "entity": ["nr"],
        "intent": ["enter_data"],
        "min_confidence": 0
    }

    def __init__(self, component_config=None):
        super(EntityEditIntent, self).__init__(component_config)

    def process(self, message, **kwargs):
        # type: (Message, **Any) -> None

        last_entities = message.get("entities", [])
        last_intent = message.get("intent", {})
    
        if last_intent["confidence"] <= self.component_config["min_confidence"]:
            for item in last_entities:
                if item["entity"] in self.component_config["entity"]:
                    entity_index = self.component_config["entity"].index(item["entity"])
                    intent_name = self.component_config["intent"][entity_index]

                    intent = {"name": intent_name, "confidence": 1.0}

                    message.set("intent", intent, add_to_output=True)

上面就是代码实现,很简单,主要配置文件就是entity、intent和min_confidence,如果confidence <= min_confidence就会根据entity来修改intent,一一对应的关系,具体的yml配置示例如下:

- name: "entity_edit_intent"
  entity: ["nr", "phone_number"]
  intent: ["enter_data", "enter_data"]
  min_confidence: 0

intent_featurizer_wordvector

正如上面说的,entity_edit_intent这个组件是强制修改intent,所以慎用。后面我就在思考如何更好的改善intent的识别问题。翻看rasa-nlu的源码,从中找到了intent为什么不是很理想的原因。在官方的component和例子中,在做intent_classifier_tensorflow_embedding识别前的word featurizer的时候,官方用的是word-of-bag做的词嵌入,只是单纯的考虑了词频,没有考虑到词义而且还有one-hot的通病稀疏矩阵的问题。所以很快想到了词向量来替换词袋法做词嵌入。
所以在intent_featurizer_wordvector这个组件中,我尝试并实现了2种词向量的方法(准确的说是两类),既可以做word2vec、glove、fasttext这一类pre-trained的词向量,也可以实现现阶段比较火的elmo词向量,具体的区别这里不详细讲了,就是elmo会根据双向神经网络模型动态计算词向量,而原始的是已经训练好的词向量直接lookup。然后这里我单纯展示具体的配置和用法。word2vec的yml配置如下:

- name: "intent_featurizer_wordvector"
  vector: "data/2000000-small.txt"
  limit: 50000

需要有已经训练好的word2vec或者glove格式的提前训练好的词向量文本,这里的limit是限制词向量的大小的,词向量是从高频到低频排序,如果词向量太大比如腾讯开源的有14G,我们就很难使用,所以这里会限制只取前面一定数量的高频词汇。elmo的yml配置如下:

- name: "intent_featurizer_wordvector"
  elmo: "data/xinhua_elmo_trained_model/"

elmo的配置需要pre_trained的网络模型,这里用的是哈工大预训练的ELMoForManyLangs模型,elmo的训练也是十分消耗硬件的,我还没有自己训练。

总结

随着项目的不断改善,需要不断地自定义一些项目需要的rasa-nlu component组件。其实rasa-nlu更多地我看成是一个框架,至于里面的模型我们可以自己填充。所以欢迎大家使用rasa-nlu-gao,直接pip install rasa-nlu-gao现在已经更新到0.1.5版本,也希望大家能够往里贡献自定义组件。原创文章,转载请说明出处

相关文章

  • rasa对话系统踩坑记(九)

    实现在rasa-core中给policy提速 之前在rasa对话系统踩坑记(六)中提到过EmbeddingPoli...

  • rasa对话系统踩坑记(三)

    在rasa对话系统踩坑记(二)中我自定义过两个component组件。也好久没更新采坑系列了,随着项目的进展迭代最...

  • rasa资源收集

    简书文章 rasa对话系统踩坑记 官方视频教程笔记 生成NLU训练数据: 1. npm install -g ch...

  • rasa对话系统踩坑记(八)

    如何将bert应用在rasa-nlu-gao 将BERT应用到rasa-nlu-gao,其实在rasa对话系统踩坑...

  • rasa对话系统踩坑记(一)

    前言 最近接到了一个任务,要做遗传方面的医疗诊断对话系统。经过前期的调研最终决定使用rasa_nlu和rasa_c...

  • rasa对话系统踩坑记(二)

    rasa_nlu模型训练 当我们准备好了rasa_nlu需要的训练数据后,就可以开始训练rasa_nlu模型。gi...

  • rasa对话系统踩坑记(四)

    前面几篇介绍了rasa-nlu-gao中自定义的几个模块,最近也没有怎么新增特别的模块,只是在词向量这块不断在尝试...

  • rasa对话系统踩坑记(五)

    上篇谈到增加了第三方闲聊的功能,避免了对话的时候遇到能识别意图之外的问题而回答“不好意思,我听不懂”这样的默认设置...

  • rasa对话系统踩坑记(七)

    如何在rasa-core框架下使用第三方nlu服务 现阶段我们rasa-core都是配合rasa-nlu使用的,但...

  • rasa对话系统踩坑记(十)

    前言 2018年底了,rasa对话系统踩坑记系列文章最后一篇,刚好是十,也算是十全十美。看似像凑数的,但这篇绝对也...

网友评论

    本文标题:rasa对话系统踩坑记(三)

    本文链接:https://www.haomeiwen.com/subject/hafgtqtx.html