首页 > 解决方案 > Rasa:如何为自定义组件提供超参数?

问题描述

我正在创建一个自定义组件以将其添加到 Rasa NLU 管道中。为此,我觉得有必要通过管道配置添加参数/超参数,就像我们为“WhitespaceTokenizer”这样的内置管道添加参数/超参数,如下所示。

pipeline:
- name: "WhitespaceTokenizer"
  intent_split_symbol: "_"

上述管道将“intent_split_symbol”作为“WhitespaceTokenizer”的超参数。同样,我想为我的管道提供超参数。

例如。

pipeline:
- name: "MyCustomComponent"
  model: "en_user_convo"

如何在“MyCustomComponent”自定义组件中提供“model”超参数?

标签: pythonrasa-nlurasa-corerasa

解决方案


您可以使用与预定义组件相同的方式为您的组件定义配置,例如

- name: "MyCustomComponent"
  param1: "test"
  param2: "second param"

你的组件的构造函数应该接受一个字典作为参数:

from rasa.nlu.components import Component

class MyCustomComponent(Component):

def __init__(self, component_config: Optional[Dict[Text, Any]] = None) -> None:
   if component_config is not None:
       param1 = component_config.get("param1")
       # ...
       super().__init__(component_config)

# Rest of the component implementation
# ...

那有意义吗?


推荐阅读