rasa train nlu详解:1.2-_train_graph()函数

2023-11-12 01:36

本文主要是介绍rasa train nlu详解:1.2-_train_graph()函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  本文使用《使用ResponseSelector实现校园招聘FAQ机器人》中的例子,主要详解介绍_train_graph()函数中变量的具体值。

一.rasa/model_training.py/_train_graph()函数
  _train_graph()函数实现,如下所示:

def _train_graph(file_importer: TrainingDataImporter,training_type: TrainingType,output_path: Text,fixed_model_name: Text,model_to_finetune: Optional[Union[Text, Path]] = None,force_full_training: bool = False,dry_run: bool = False,**kwargs: Any,
) -> TrainingResult:if model_to_finetune:  # 如果有模型微调model_to_finetune = rasa.model.get_model_for_finetuning(model_to_finetune)  # 获取模型微调if not model_to_finetune:  # 如果没有模型微调rasa.shared.utils.cli.print_error_and_exit(  # 打印错误并退出f"No model for finetuning found. Please make sure to either "   # 没有找到微调模型。请确保f"specify a path to a previous model or to have a finetunable " # 要么指定一个以前模型的路径,要么有一个可微调的f"model within the directory '{output_path}'."                  # 在目录'{output_path}'中的模型。)rasa.shared.utils.common.mark_as_experimental_feature(  # 标记为实验性功能"Incremental Training feature"  # 增量训练功能)is_finetuning = model_to_finetune is not None  # 如果有模型微调config = file_importer.get_config()  # 获取配置recipe = Recipe.recipe_for_name(config.get("recipe"))  # 获取配方config, _missing_keys, _configured_keys = recipe.auto_configure(  # 自动配置file_importer.get_config_file_for_auto_config(),  # 获取自动配置的配置文件config,  # 配置training_type,  # 训练类型)model_configuration = recipe.graph_config_for_recipe(  # 配方的graph配置config,  # 配置kwargs,  # 关键字参数training_type=training_type,  # 训练类型is_finetuning=is_finetuning,  # 是否微调)rasa.engine.validation.validate(model_configuration)  # 验证tempdir_name = rasa.utils.common.get_temp_dir_name()  # 获取临时目录名称# Use `TempDirectoryPath` instead of `tempfile.TemporaryDirectory` as this leads to errors on Windows when the context manager tries to delete an already deleted temporary directory (e.g. https://bugs.python.org/issue29982)# 翻译:使用TempDirectoryPath而不是tempfile.TemporaryDirectory,因为当上下文管理器尝试删除已删除的临时目录时,这会导致Windows上的错误(例如https://bugs.python.org/issue29982)with rasa.utils.common.TempDirectoryPath(tempdir_name) as temp_model_dir:  # 临时模型目录model_storage = _create_model_storage(  # 创建模型存储is_finetuning, model_to_finetune, Path(temp_model_dir)  # 是否微调,模型微调,临时模型目录)cache = LocalTrainingCache()  # 本地训练缓存trainer = GraphTrainer(model_storage, cache, DaskGraphRunner)  # Graph训练器if dry_run:  # dry运行fingerprint_status = trainer.fingerprint(                        # fingerprint状态model_configuration.train_schema, file_importer              # 模型配置的训练模式,文件导入器)return _dry_run_result(fingerprint_status, force_full_training)  # 返回dry运行结果model_name = _determine_model_name(fixed_model_name, training_type)  # 确定模型名称full_model_path = Path(output_path, model_name)                # 完整的模型路径with telemetry.track_model_training(                    # 跟踪模型训练file_importer, model_type=training_type.model_type  # 文件导入器,模型类型):trainer.train(                               # 训练model_configuration,                     # 模型配置file_importer,                           # 文件导入器full_model_path,                         # 完整的模型路径force_retraining=force_full_training,    # 强制重新训练is_finetuning=is_finetuning,             # 是否微调)rasa.shared.utils.cli.print_success(         # 打印成功f"Your Rasa model is trained and saved at '{full_model_path}'."  # Rasa模型已经训练并保存在'{full_model_path}'。)return TrainingResult(str(full_model_path), 0)   # 训练结果

1.传递来的形参数据

2._train_graph()函数组成
  该函数主要由3个方法组成,如下所示:

  • model_configuration = recipe.graph_config_for_recipe(*)
  • trainer = GraphTrainer(model_storage, cache, DaskGraphRunner)
  • trainer.train(model_configuration, file_importer, full_model_path, force_retraining, is_finetuning)

二._train_graph()函数中的方法
1.file_importer.get_config()
  将config.yml文件转化为dict类型,如下所示:

2.Recipe.recipe_for_name(config.get(“recipe”))

(1)ENTITY_EXTRACTOR = ComponentType.ENTITY_EXTRACTOR
实体抽取器。
(2)INTENT_CLASSIFIER = ComponentType.INTENT_CLASSIFIER
意图分类器。
(3)MESSAGE_FEATURIZER = ComponentType.MESSAGE_FEATURIZER
消息特征化。
(4)MESSAGE_TOKENIZER = ComponentType.MESSAGE_TOKENIZER
消息Tokenizer。
(5)MODEL_LOADER = ComponentType.MODEL_LOADER
模型加载器。
(6)POLICY_WITHOUT_END_TO_END_SUPPORT = ComponentType.POLICY_WITHOUT_END_TO_END_SUPPORT
非端到端策略支持。
(7)POLICY_WITH_END_TO_END_SUPPORT = ComponentType.POLICY_WITH_END_TO_END_SUPPORT
端到端策略支持。

3.model_configuration = recipe.graph_config_for_recipe(*)
  model_configuration.train_schema和model_configuration.predict_schema的数据类型都是GraphSchema类对象,分别表示在训练和预测时所需要的SchemaNode,以及SchemaNode在GraphSchema中的依赖关系。

(1)model_configuration.train_schema

  • schema_validator:rasa.graph_components.validators.default_recipe_validator.DefaultV1RecipeValidator类中的validate方法
  • finetuning_validator:rasa.graph_components.validators.finetuning_validator.FinetuningValidator类中的validate方法
  • nlu_training_data_provider:rasa.graph_components.providers.nlu_training_data_provider.NLUTrainingDataProvider类中的provide方法
  • train_JiebaTokenizer0:rasa.nlu.tokenizers.jieba_tokenizer.JiebaTokenizer类中的train方法
  • run_JiebaTokenizer0:rasa.nlu.tokenizers.jieba_tokenizer.JiebaTokenizer类中的process_training_data方法
  • run_LanguageModelFeaturizer1:rasa.nlu.featurizers.dense_featurizer.lm_featurizer.LanguageModelFeaturizer类中的process_training_data方法
  • train_DIETClassifier2:rasa.nlu.classifiers.diet_classifier.DIETClassifier类中的train方法
  • train_ResponseSelector3:rasa.nlu.selectors.response_selector.ResponseSelector类中的train方法

说明:ResponseSelector类继承自DIETClassifier类。

(2)model_configuration.predict_schema

  • nlu_message_converter:rasa.graph_components.converters.nlu_message_converter.NLUMessageConverter类中的convert_user_message方法
  • run_JiebaTokenizer0:rasa.nlu.tokenizers.jieba_tokenizer.JiebaTokenizer类中的process方法
  • run_LanguageModelFeaturizer1:rasa.nlu.featurizers.dense_featurizer.lm_featurizer.LanguageModelFeaturizer类中的process方法
  • run_DIETClassifier2:rasa.nlu.classifiers.diet_classifier.DIETClassifier类中的process方法
  • run_ResponseSelector3:rasa.nlu.selectors.response_selector.ResponseSelector类中的process方法
  • run_RegexMessageHandler:rasa.nlu.classifiers.regex_message_handler.RegexMessageHandler类中的process方法

4.tempdir_name
  ‘C:\Users\ADMINI~1\AppData\Local\Temp\tmpg0v179ea’

5.trainer = GraphTrainer(*)和trainer.train(*)
  这里执行的代码是rasa/engine/training/graph_trainer.py中GraphTrainer类的train()方法,实现功能为训练和打包模型并返回预测graph运行程序。

6.Rasa中GraphComponent的子类


参考文献:
[1]https://github.com/RasaHQ/rasa
[2]rasa 3.2.10 NLU模块的训练:https://zhuanlan.zhihu.com/p/574935615
[3]rasa.engine.graph:https://rasa.com/docs/rasa/next/reference/rasa/engine/graph/

这篇关于rasa train nlu详解:1.2-_train_graph()函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/394195

相关文章

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五