RASA-特征生成组件Featurizer

      RASA文本特征生成器分为两个不同类别:稀疏特征生成器如One-hot和密集特征生成器如Bert。稀疏特征生成器会返回具大量缺失值(例如零)的特征向量。但是由于这些特征向量通常会占用大量内存,因此我们将它们存储为稀疏特征,稀疏特征仅存储非零值及其在向量中的位置,能够在更大的数据集上进行训练。

      所有特征生成器都可以返回两种不同类型的特征:序列特征和句子特征。序列特征是 维度为number-of-tokens x feature-dimension 的矩阵。该矩阵包含序列中每个标记的特征向量。我们用这个特征去训练序列模型,如实体识别。句子特征是纬度为(1 x feature-dimension) 的矩阵,包含完整话语的特征向量,可以用来做意图分类。句子特征可用于任何词袋模型。具体使用哪种特征方法由使用的分类器决定。注意:feature-dimensionfor 序列和句子的特征不必相同。


MitieFeaturizer

      该特征生成器输出为稠密向量,可以用作实体提取、意图分类和响应分类的特征使用。需要在pipeline中引入MitieNLP语言模型。但是有意思的是,该特征器并没有被MitieIntentClassifier使用,因为MitieIntentClassifier里面实现了所有分词,特征提取功能。

      MitieFeaturizer是对每个Token输出一个feature-dimension维度的向量,那么生成句子向量的做法是通过pooling技术,这里可以选择max pooling和mean pooling,这个参数可以在配置文件中指定。最终会生成一个1 x feature-dimension的句子向量。max pooling算法就是取每个token中相同维度,最大的值作为句子向量的这个维度的值,那么mean pooling就好理解了,句子向量是每个token的均值。

      MitieFeaturizer主要是在pipline里面进行配置,配置方式如下:

pipeline:
- name: "MitieFeaturizer"
  # Specify what pooling operation should be used to calculate the vector of
  # the complete utterance. Available options: 'mean' and 'max'.
  "pooling": "mean"

SpacyFeaturizer

      该特征生成器输出为稠密向量,可以用作实体提取、意图分类和响应分类的特征使用。需要在pipeline中引入SpacyNLP语言模型。

      SpacyFeaturizer是对每个Token输出一个feature-dimension维度的向量,那么生成句子向量的做法是通过pooling技术,这里可以选择max pooling和mean pooling,这个参数可以在配置文件中指定。最终会生成一个1 x feature-dimension的句子向量。max pooling算法就是取每个token中相同维度,最大的值作为句子向量的这个维度的值,那么mean pooling就好理解了,句子向量是每个token的均值。

      SpacyFeaturizer主要是在pipline里面进行配置,配置方式如下:

pipeline:
  - name:"SpacyNLP"
     model:"en_core_web_md"
  - name: "SpacyTokenizer"
    "intent_tokenization_flag": False
    "intent_split_symbol": "+"
    "token_pattern": None
  - name: "SpacyFeaturizer"
    # Specify what pooling operation should be used to calculate the vector of
    # the complete utterance. Available options: 'mean' and 'max'.
    "pooling": "mean"

ConveRTFeaturizer

      ConveRT模型生成句子的特征表示,输出为稠密变量。因为ConveRT模型只有英文模型,因此中文对话机器人不能使用ConveRTFeaturizer,除非你想自己训练一个ConveRT模型。使用该Featurizer的时候,需要使用model_url参数配置模型文件的路径,否则训练的时候会报错。

      ConveRTFeaturizer已经实现ConveRTTokenizer的功能,所以Pipeline可以配置任意的Tokenizer。从代码里面看ConveRTTokenizer,LanguageModelTokenizer都是继承WhitespaceTokenizer,并没有做特殊处理,而用WhitespaceTokenizer在中文的时候,会提示不支持中文错误,所以可选的分词器并不多,只有MitieTokenizer和SpcayTokenizer。

      注意:要使用ConveRTFeaturizer,请安装 Rasa pip3 install rasa[convert]。

扫描二维码关注公众号,回复: 15064260 查看本文章

      配置方式如下:

pipeline:
- name: "ConveRTFeaturizer"
# Remote URL/Local directory of model files(Required)
"model_url": None

LanguageModelFeaturizer    

     这就是通过预训练模型来作为特征生成器,输入为用户消息,响应消息等,输出为稠密变量。使用LanguageModelFeaturizer,首先要根据机器人的是中文的还是英文的,选择不同的预训练模型。下表是目前支持的预训练模型。因为LanguageModelFeaturizer里面已经实现了分词功能,因为pipeline里面可以配置任意Tokenizer。但注意要是稠密向量,所以不能配置WhitespaceTokenizer和JiebaTokenizer。而LanguageModelTokenizer又不支持中文,所以只有MitieTokenizer和SpcayTokenizer。这里配置什么分词器并不影响最终结果。

      LanguageModelFeaturizer支持的预训练模型有:

      配置方式如下:

pipeline:
  - name: LanguageModelFeaturizer
    # Name of the language model to use
    model_name: "bert"
    # Pre-Trained weights to be loaded
    model_weights: "rasa/LaBSE"

    # An optional path to a directory from which
    # to load pre-trained model weights.
    # If the requested model is not found in the
    # directory, it will be downloaded and
    # cached in this directory for future use.
    # The default value of `cache_dir` can be
    # set using the environment variable
    # `TRANSFORMERS_CACHE`, as per the
    # Transformers library.
    cache_dir: null

RegexFeaturizer

      使用正则表达式创建一个消息表示,注意输入只能是用户消息,这个和其他几个Featurizer不同,输出的内容是一个稀疏向量。

      在训练阶段,根据配置会生成一个符合输入句子的正则表达式列表,当运行阶段,针对用户消息,会对每一个正则表达式都匹配一次,如果匹配这个正则表达式就会创建一个特征,然后所有这些特征形成一个列表,输出到分类器和提取器里面。那么分类器和实体提取器会根据这个特征指示知道已经找到分类标签或者实体了,然后会直接使用分类标签和实体。

      注意,目前只有CRFEntityExtractor和DIETClassifier这两个才支持RegexFeaturizer。

      RegexFeaturizer在pipeline里面配置,具体正则表达式的配置在训练数据里面配置。通过添加选项使featurizer 不区分大小写case_sensitive: False,默认为 case_sensitive: True.

      要正确处理中文等不使用空格分隔单词的语言,用户需要添加该use_word_boundaries: False选项,默认为use_word_boundaries: True.

      配置如下:

pipeline:
- name: "RegexFeaturizer"
  # Text will be processed with case sensitive as default
  "case_sensitive": True
  # use match word boundaries for lookup table
  "use_word_boundaries": True

CountVectorsFeaturizer

      把用户消息,意图,响应消息创建一个词袋模型表示。输出也是稀疏矩阵。

      CountVectorizer生成算法是使用sklearn’s CountVectorizer。配置参数具体可以参考sklearn’s CountVectorizer的文档。

      配置如下:

pipeline:
- name: CountVectorsFeaturizer
  additional_vocabulary_size:
    text: 1000
    response: 1000
    action_text: 1000

LexicalSyntacticFeaturizer

      将输入的消息创建词法和语法特征,用于后续的实体识别。输入是用户输入消息,输出是稀疏向量。


==============  ==========================================================================================
Feature Name    Description
==============  ==========================================================================================
BOS             Checks if the token is at the beginning of the sentence.
EOS             Checks if the token is at the end of the sentence.
low             Checks if the token is lower case.
upper           Checks if the token is upper case.
title           Checks if the token starts with an uppercase character and all remaining characters are
                lowercased.
digit           Checks if the token contains just digits.
prefix5         Take the first five characters of the token.
prefix2         Take the first two characters of the token.
suffix5         Take the last five characters of the token.
suffix3         Take the last three characters of the token.
suffix2         Take the last two characters of the token.
suffix1         Take the last character of the token.
pos             Take the Part-of-Speech tag of the token (``SpacyTokenizer`` required).
pos2            Take the first two characters of the Part-of-Speech tag of the token
                (``SpacyTokenizer`` required).
==============  ==========================================================================================

      特征提取器是后续计算的基础,信息越多越有利于后面的计算,所以常常都是几种办法组合使用。尤其RegexFeaturizer是最常用的。LexicalSyntacticFeaturizer也会提供一些有益的信息。值得注意的是,在后续说到的分类器会对特征有要求,是稀疏向量还是稠密向量。

      当特征化器使用滑动窗口在用户消息中的标记上移动时,开发者可以为滑动窗口中的先前标记、当前标记和下一个标记定义特征。开发者将功能定义为[before, token, after]数组。如果想为之前的令牌、当前令牌和之后的令牌定义功能,具体配置将如下所示:

pipeline:
- name: LexicalSyntacticFeaturizer
  "features": [
    ["low", "title", "upper"],
    ["BOS", "EOS", "low", "upper", "title", "digit"],
    ["low", "title", "upper"],
  ]

主要来源:

  1. https://blog.csdn.net/qq_35273499/article/details/128456306

  1. https://rasa.com/docs/rasa/components/#dietclassifier-2

猜你喜欢

转载自blog.csdn.net/fzz97_/article/details/128880089