模型训练#

ACE Agent 快速入门 配备了一个模型助手脚本,可以轻松地使用自定义领域特定的数据集训练 NVIDIA Riva Joint Intent & Slot Classification(联合意图和槽位分类)、文本分类和命名实体识别 NLP 模型,评估模型并部署它们。在底层,ACE Agent 使用 NVIDIA TAO 工具包进行模型训练,并使用 Riva Skills 进行模型部署。

目前,联合意图和槽位模型训练同时支持基于 yaml 的训练数据集和 NeMo Joint Intent & Slot 数据集。对于文本分类器和 NER 训练,仅允许使用 NeMo 格式数据集。运行以下命令,从 ACE Agent 快速入门资源目录 获取模型实用程序中受支持任务的详细参数和文档。

source deploy/docker/docker_init.sh

docker compose -f deploy/docker/docker-compose.yml run model-utils -h

ACE Agent 提供了一个示例管线,作为 快速入门脚本 的一部分,位于 samples/training 目录中。该示例管线执行以下任务

  • 以单个 NEMO 数据集格式导出多个领域数据集。

  • 训练模型。

  • 评估训练后的模型。

  • 对示例查询推断训练后的模型。

  • 将训练后的 TLT 模型导出为 .riva.rmir 格式。

  • [可选] 将 RMIR 模型上传到 NGC。

以下步骤训练用于食品订购或快餐餐厅领域的联合意图和槽位识别器。以下训练后的模型可以与示例食品订购 Bot 一起使用。

  1. 准备训练数据。

    要添加意图,我们需要添加查询,这些查询是您可能向 Bot 提出的问题的示例。您应该为每个意图提供多个训练示例,以便您的 Bot 可以理解各种表达方式。

    nlu_data/qsr_assistant.yaml 创建一个文件,并添加与 add_itemquery_itemcontextual_queryyes_intentno_intentcheckout 意图相关的查询。为所有意图注释实体。在示例查询中,使用方括号添加实体值,并在大括号中添加实体名称,两者之间没有空格。您可以向以下列表添加更多变体。

    version: "1.0"
    domain: qsr
    nlu:
    - intent: add_item
    queries: |
        - Add [french fries]{food_name}
        - Can I get a [cheeseburger]{food_name}
        - Please add [avocado]{food_name} to my cart
        - Add it please
        - Add [two]{food_quantity} [small]{food_size} ones please
        - Add a [medium]{food_size} [double protein burger]{food_name} and [two]{food_quantity} [sandwiches]{food_name}
        - Can I get a [extra large]{food_size} [cola]{food_name}
        - Please add [avocado]{food_name} to my cart
        - Add it please
        - Add [two]{food_quantity} [small]{food_size} ones please
    - intent: query_item
    queries: |
        - How much calories [french fries]{food_name} has
        - What all is there in [cheeseburger]{food_name}
        - How much does a [regular]{food_size} [pepsi]{food_name} cost
        - What is the [market salad]{food_name} made of
        - what ingredients are there in [cobb salad]{food_name}
        - what sizes is the [chicken sandwich]{food_name} available in
        - What toppings are there with the [pizza]{food_name}
        - what are [chicken nuggets]{food_name}
        - What healthy options do you have in menu
        - What sides are there in menu
        - show me your popular options
        - tell me the best selling items in menu
        - show me your drinks menu
        - what desserts do you have
    - intent: contextual_query
    queries: |
        - [french fries]{food_name}
        - [cheeseburger]{food_name} please
        - [two]{food_quantity} [small]{food_size} [pepsi]{food_name}
        - what about [sandwiches]{food_name}
        - what about [lemon tea]{food_name}
        - what about [french toast]{food_name}
        - umm [diet cola]{food_name}
        - [lemonade]{food_name}
        - [medium]{food_size} [burgers]{food_name}
        - [nuggets]{food_name} in [small]{food_size} size
    - intent: checkout
    queries: |
        - That's all
        - I would like to checkout now
        - complete the order
        - checkout please
        - nothing else for today
        - nothing else please
        - All done for today
        - I am all set
        - that completes my order
        - let's checkout now
    - intent: yes_intent
    queries: |
        - yes
        - Yup
        - yes confirmed
        - yes please
        - please yes
        - yup yup
        - yes yes
        - sure yes
        - yes let's go
        - sure
    - intent: no_intent
    queries: |
        - no
        - nope
        - no no
        - no please
        - no wait
        - nope nope
        - please no
    
  2. 训练联合意图和槽位模型。

    准备好训练数据后,下一步是使用 ACE Agent 触发领域模型训练。

    ACE Agent 在 samples/training/pipeline_intent_slot.yaml 中提供了一个示例管线,作为 快速入门脚本 的一部分,用于训练联合意图和槽位分类模型,将其导出为所需格式,并将模型上传到 NGC。我们需要更新所有标记为 {UPDATE} 的字段,并根据训练要求选择性地更新其他参数。

    samples/training/pipeline_intent_slot.yaml 文件进行以下更改,以便触发 qsr 领域的训练。

    1. {UPDATE} 替换为以下参数

      model_name: qsr
      dataset_name: qsr_intent_slot_dataset
      dataset_paths: [ "nlu_data/qsr_assistant.yaml"]
      
    2. 更新模型需要存储在 global 下的路径

      global:
          result_path: "./results/models"
      
    3. 更新训练超参数。

      - _name: task_train_intent_slot_1
        epochs: 25
        batch_size: 1
      
    4. [可选] 注释掉 task_upload 块,因为我们不想将模型上传到 NGC。

      如果模型需要在训练步骤结束时存储在 NGC 中,请提供正确的 NGC 路径,如下所示

      - _name: task_upload_model_7
        ngc_path: ngc_org/[ngc_team/]model_name:version
      
    5. 在对 samples/training/pipeline_intent_slot.yaml 进行上述更改后,使用 快速入门脚本 触发 intent_slot 模型训练。由于数据集较小,我们使用了较小的批量大小和 epoch。

      source deploy/docker/docker_init.sh
      
      docker compose -f deploy/docker/docker-compose.yml run model-utils pipeline --config_yaml samples/training/pipeline_intent_slot.yaml
      
    6. 训练结束后,确认 RMIR 格式的联合意图和槽位模型文件已在 result 目录中生成。

      find results/models/ -name *.rmir
      Output:       results/models/qsr/1/model/intent_slot_qsr.rmir
      
  3. 部署联合意图和槽位模型。

    您可以使用 ACE Agent NLP 服务器部署联合意图和槽位模型,方法是创建一个 model_config.yaml 文件。

    model_servers:
    - name: riva
        nlp_models:
        - ./results/models/qsr/1/model/intent_slot_qsr.rmir
        url: localhost:8001
        riva_url: localhost:50051
    
  4. 部署模型。

    export BOT_PATH="."
    source deploy/docker/docker_init.sh
    
    docker compose -f deploy/docker/docker-compose.yml up model-utils
    

注意

由于我们使用了小型数据集进行演示,因此模型的准确性可能较低。如果使用更大的数据集,请确保您熟悉这些最佳实践

  • 每个意图和实体应至少添加 20 条话语。

  • 避免使用自动生成训练数据的工具或数据集格式来快速生成大量示例。这可能会导致模型过拟合。

  • 每个意图应仅包含唯一的话语,并且不应与其他意图重叠。

  • 始终包含超出范围的领域或意图,以避免 Bot 对不支持的查询做出不正确的响应。

  • 添加包含拼写错误和简写的话语,以匹配用户对话语言。

或者,您可以按照 NeMo 教程 训练模型,使用 Riva 自定义模型文档 中的说明创建 RMIR 格式模型文件,并在 NLP 服务器模型部署中使用它。