async def train_core_async( domain: Union[Domain, Text], config: Text, stories: Text, output: Text, train_path: Optional[Text] = None, fixed_model_name: Optional[Text] = None, additional_arguments: Optional[Dict] = None, model_to_finetune: Optional[Text] = None, finetuning_epoch_fraction: float = 1.0, ) -> Optional[Text]: """Trains a Core model. Args: domain: Path to the domain file. config: Path to the config file for Core. stories: Path to the Core training data. output: Output path. train_path: If `None` the model will be trained in a temporary directory, otherwise in the provided directory. fixed_model_name: Name of model to be stored. additional_arguments: Additional training parameters. model_to_finetune: Optional path to a model which should be finetuned or a directory in case the latest trained model should be used. finetuning_epoch_fraction: The fraction currently specified training epochs in the model configuration which should be used for finetuning. Returns: If `train_path` is given it returns the path to the model archive, otherwise the path to the directory with the trained model files. """ file_importer = TrainingDataImporter.load_core_importer_from_config( config, domain, [stories]) domain = await file_importer.get_domain() if domain.is_empty(): rasa.shared.utils.cli.print_error( "Core training was skipped because no valid domain file was found. " "Please specify a valid domain using '--domain' argument or check " "if the provided domain file exists.") return None if not await file_importer.get_stories(): rasa.shared.utils.cli.print_error( "No stories given. Please provide stories in order to " "train a Rasa Core model using the '--stories' argument.") return return await _train_core_with_validated_data( file_importer, output=output, train_path=train_path, fixed_model_name=fixed_model_name, additional_arguments=additional_arguments, model_to_finetune=model_to_finetune, finetuning_epoch_fraction=finetuning_epoch_fraction, )
async def train_comparison_models( story_file: Text, domain: Text, output_path: Text = "", exclusion_percentages: Optional[List] = None, policy_configs: Optional[List] = None, runs: int = 1, additional_arguments: Optional[Dict] = None, ) -> None: """Train multiple models for comparison of policies""" from rasa import model exclusion_percentages = exclusion_percentages or [] policy_configs = policy_configs or [] for r in range(runs): logging.info("Starting run {}/{}".format(r + 1, runs)) for current_run, percentage in enumerate(exclusion_percentages, 1): for policy_config in policy_configs: file_importer = TrainingDataImporter.load_core_importer_from_config( policy_config, domain, [story_file] ) config_name = os.path.splitext(os.path.basename(policy_config))[0] logging.info( "Starting to train {} round {}/{}" " with {}% exclusion" "".format( config_name, current_run, len(exclusion_percentages), percentage ) ) with TempDirectoryPath(tempfile.mkdtemp()) as train_path: _, new_fingerprint = await asyncio.gather( train( domain, file_importer, train_path, policy_config=policy_config, exclusion_percentage=percentage, additional_arguments=additional_arguments, ), model.model_fingerprint(file_importer), ) output_dir = os.path.join(output_path, "run_" + str(r + 1)) model_name = config_name + PERCENTAGE_KEY + str(percentage) model.package_model( fingerprint=new_fingerprint, output_directory=output_dir, train_path=train_path, fixed_model_name=model_name, )
async def train_core_async( domain: Union[Domain, Text], config: Text, stories: Text, output: Text, train_path: Optional[Text] = None, fixed_model_name: Optional[Text] = None, additional_arguments: Optional[Dict] = None, ) -> Optional[Text]: """Trains a Core model. Args: domain: Path to the domain file. config: Path to the config file for Core. stories: Path to the Core training data. output: Output path. train_path: If `None` the model will be trained in a temporary directory, otherwise in the provided directory. fixed_model_name: Name of model to be stored. uncompress: If `True` the model will not be compressed. additional_arguments: Additional training parameters. Returns: If `train_path` is given it returns the path to the model archive, otherwise the path to the directory with the trained model files. """ file_importer = TrainingDataImporter.load_core_importer_from_config( config, domain, [stories]) domain = await file_importer.get_domain() if domain.is_empty(): print_error( "Core training was skipped because no valid domain file was found. " "Please specify a valid domain using '--domain' argument or check if the provided domain file exists." ) return None if not await file_importer.get_stories(): print_error("No stories given. Please provide stories in order to " "train a Rasa Core model using the '--stories' argument.") return return await _train_core_with_validated_data( file_importer, output=output, train_path=train_path, fixed_model_name=fixed_model_name, additional_arguments=additional_arguments, )
async def test_core_only(project: Text): config_path = os.path.join(project, DEFAULT_CONFIG_PATH) domain_path = os.path.join(project, DEFAULT_DOMAIN_PATH) default_data_path = os.path.join(project, DEFAULT_DATA_PATH) actual = TrainingDataImporter.load_core_importer_from_config( config_path, domain_path, training_data_paths=[default_data_path]) assert isinstance(actual, CoreDataImporter) stories = await actual.get_stories() assert not stories.is_empty() domain = await actual.get_domain() assert not domain.is_empty() config = await actual.get_config() assert config nlu_data = await actual.get_nlu_data() assert nlu_data.is_empty()
def train_core( domain: Union[Domain, Text], config: Text, stories: Text, output: Text, fixed_model_name: Optional[Text] = None, additional_arguments: Optional[Dict] = None, model_to_finetune: Optional[Text] = None, finetuning_epoch_fraction: float = 1.0, ) -> Optional[Text]: """Trains a Core model. Args: domain: Path to the domain file. config: Path to the config file for Core. stories: Path to the Core training data. output: Output path. fixed_model_name: Name of model to be stored. additional_arguments: Additional training parameters. model_to_finetune: Optional path to a model which should be finetuned or a directory in case the latest trained model should be used. finetuning_epoch_fraction: The fraction currently specified training epochs in the model configuration which should be used for finetuning. Returns: Path to the model archive. """ file_importer = TrainingDataImporter.load_core_importer_from_config( config, domain, [stories]) stories = file_importer.get_stories() nlu_data = file_importer.get_nlu_data() domain = file_importer.get_domain() if nlu_data.has_e2e_examples(): rasa.shared.utils.cli.print_error( "Stories file contains e2e stories. Please train using `rasa train` so that" " the NLU model is also trained.") return None if domain.is_empty(): rasa.shared.utils.cli.print_error( "Core training was skipped because no valid domain file was found. " "Please specify a valid domain using '--domain' argument or check " "if the provided domain file exists.") return None if not stories: rasa.shared.utils.cli.print_error( "No stories given. Please provide stories in order to " "train a Rasa Core model using the '--stories' argument.") return None return _train_graph( file_importer, training_type=TrainingType.CORE, output_path=output, model_to_finetune=model_to_finetune, fixed_model_name=fixed_model_name, finetuning_epoch_fraction=finetuning_epoch_fraction, **(additional_arguments or {}), ).model