def _load_model_class( cls, tf_model_file: Text, model_data_example: RasaModelData, label_data: RasaModelData, entity_tag_specs: List[EntityTagSpec], config: Dict[Text, Any], finetune_mode: bool = False, ) -> "RasaModel": predict_data_example = RasaModelData( label_key=model_data_example.label_key, data={ feature_name: features for feature_name, features in model_data_example.items() if TEXT in feature_name }, ) return cls.model_class(config[USE_TEXT_AS_LABEL]).load( tf_model_file, model_data_example, predict_data_example, data_signature=model_data_example.get_signature(), label_data=label_data, entity_tag_specs=entity_tag_specs, config=copy.deepcopy(config), finetune_mode=finetune_mode, )
def __init__( self, name: Text, config: Dict[Text, Any], data_signature: Dict[Text, Dict[Text, List[FeatureSignature]]], label_data: RasaModelData, ) -> None: super().__init__( name=name, random_seed=config[RANDOM_SEED], tensorboard_log_dir=config[TENSORBOARD_LOG_DIR], tensorboard_log_level=config[TENSORBOARD_LOG_LEVEL], checkpoint_model=config[CHECKPOINT_MODEL], ) self.config = config self.data_signature = data_signature self.label_signature = label_data.get_signature() self._check_data() label_batch = label_data.prepare_batch() self.tf_label_data = self.batch_to_model_data_format( label_batch, self.label_signature) # set up tf layers self._tf_layers: Dict[Text, tf.keras.layers.Layer] = {}
def _update_data_signatures(self, model_data: RasaModelData) -> None: self.data_signature = model_data.get_signature() self.predict_data_signature = { feature_name: features for feature_name, features in self.data_signature.items() if TEXT in feature_name }
def _instantiate_model_class(self, model_data: RasaModelData) -> "RasaModel": return self.model_class(self.use_text_as_label)( data_signature=model_data.get_signature(), label_data=self._label_data, entity_tag_specs=self._entity_tag_specs, config=self.component_config, )
def load(cls, path: Text) -> "TEDPolicy": """Loads a policy from the storage. **Needs to load its featurizer** """ if not os.path.exists(path): raise Exception(f"Failed to load TED policy model. Path " f"'{os.path.abspath(path)}' doesn't exist.") model_path = Path(path) tf_model_file = model_path / f"{SAVE_MODEL_FILE_NAME}.tf_model" featurizer = TrackerFeaturizer.load(path) if not (model_path / f"{SAVE_MODEL_FILE_NAME}.data_example.pkl").is_file(): return cls(featurizer=featurizer) loaded_data = io_utils.json_unpickle( model_path / f"{SAVE_MODEL_FILE_NAME}.data_example.pkl") label_data = io_utils.json_unpickle( model_path / f"{SAVE_MODEL_FILE_NAME}.label_data.pkl") meta = io_utils.pickle_load(model_path / f"{SAVE_MODEL_FILE_NAME}.meta.pkl") priority = io_utils.json_unpickle( model_path / f"{SAVE_MODEL_FILE_NAME}.priority.pkl") model_data_example = RasaModelData(label_key=LABEL_IDS, data=loaded_data) meta = train_utils.update_similarity_type(meta) model = TED.load( str(tf_model_file), model_data_example, data_signature=model_data_example.get_signature(), config=meta, max_history_tracker_featurizer_used=isinstance( featurizer, MaxHistoryTrackerFeaturizer), label_data=label_data, ) # build the graph for prediction predict_data_example = RasaModelData( label_key=LABEL_IDS, data={ feature_name: features for feature_name, features in model_data_example.items() if DIALOGUE in feature_name }, ) model.build_for_predict(predict_data_example) return cls(featurizer=featurizer, priority=priority, model=model, **meta)
def run_training(self, model_data: RasaModelData, label_ids: Optional[np.ndarray] = None) -> None: """Feeds the featurized training data to the model. Args: model_data: Featurized training data. label_ids: Label ids corresponding to the data points in `model_data`. These may or may not be used by the function depending on how the policy is trained. """ if not self.finetune_mode: # This means the model wasn't loaded from a # previously trained model and hence needs # to be instantiated. self.model = self.model_class()( model_data.get_signature(), self.config, isinstance(self.featurizer, MaxHistoryTrackerFeaturizer), self._label_data, self._entity_tag_specs, ) self.model.compile( optimizer=tf.keras.optimizers.Adam(self.config[LEARNING_RATE])) ( data_generator, validation_data_generator, ) = rasa.utils.train_utils.create_data_generators( model_data, self.config[BATCH_SIZES], self.config[EPOCHS], self.config[BATCH_STRATEGY], self.config[EVAL_NUM_EXAMPLES], self.config[RANDOM_SEED], ) callbacks = rasa.utils.train_utils.create_common_callbacks( self.config[EPOCHS], self.config[TENSORBOARD_LOG_DIR], self.config[TENSORBOARD_LOG_LEVEL], self.tmp_checkpoint_dir, ) self.model.fit( data_generator, epochs=self.config[EPOCHS], validation_data=validation_data_generator, validation_freq=self.config[EVAL_NUM_EPOCHS], callbacks=callbacks, verbose=False, shuffle=False, # we use custom shuffle inside data generator )
def _load_model_class( cls, tf_model_file: Text, model_data_example: RasaModelData, label_data: RasaModelData, entity_tag_specs: List[EntityTagSpec], meta: Dict[Text, Any], ) -> "RasaModel": return cls.model_class(meta[USE_TEXT_AS_LABEL]).load( tf_model_file, model_data_example, data_signature=model_data_example.get_signature(), label_data=label_data, entity_tag_specs=entity_tag_specs, config=copy.deepcopy(meta), )
def __init__( self, data_signature: Dict[Text, List[FeatureSignature]], config: Dict[Text, Any], max_history_tracker_featurizer_used: bool, label_data: RasaModelData, ) -> None: super().__init__( name="TED", random_seed=config[RANDOM_SEED], tensorboard_log_dir=config[TENSORBOARD_LOG_DIR], tensorboard_log_level=config[TENSORBOARD_LOG_LEVEL], ) self.config = config self.max_history_tracker_featurizer_used = max_history_tracker_featurizer_used # data self.data_signature = data_signature self._check_data() self.predict_data_signature = { feature_name: features for feature_name, features in data_signature.items() if DIALOGUE in feature_name } # optimizer self.optimizer = tf.keras.optimizers.Adam() self.all_labels_embed = None label_batch = label_data.prepare_batch() self.tf_label_data = self.batch_to_model_data_format( label_batch, label_data.get_signature() ) # metrics self.action_loss = tf.keras.metrics.Mean(name="loss") self.action_acc = tf.keras.metrics.Mean(name="acc") self.metrics_to_log += ["loss", "acc"] # set up tf layers self._tf_layers: Dict[Text : tf.keras.layers.Layer] = {} self._prepare_layers()
def __init__( self, name: Text, config: Dict[Text, Any], data_signature: Dict[Text, Dict[Text, List[FeatureSignature]]], label_data: RasaModelData, ) -> None: super().__init__(name=name, random_seed=config[RANDOM_SEED]) self.config = config self.data_signature = data_signature self.label_signature = label_data.get_signature() self._check_data() label_batch = RasaDataGenerator.prepare_batch(label_data.data) self.tf_label_data = self.batch_to_model_data_format( label_batch, self.label_signature) # set up tf layers self._tf_layers: Dict[Text, tf.keras.layers.Layer] = {}
def _load_tf_model( cls, model_utilities: Dict[Text, Any], model_data_example: RasaModelData, predict_data_example: RasaModelData, featurizer: TrackerFeaturizer, should_finetune: bool, ) -> "TED": model = cls.model_class().load( str(model_utilities["tf_model_file"]), model_data_example, predict_data_example, data_signature=model_data_example.get_signature(), config=model_utilities["meta"], max_history_featurizer_is_used=isinstance( featurizer, MaxHistoryTrackerFeaturizer), label_data=model_utilities["label_data"], entity_tag_specs=model_utilities["entity_tag_specs"], finetune_mode=should_finetune, ) return model
def _load_model( cls, index_label_id_mapping: Dict[int, Text], index_tag_id_mapping: Dict[int, Text], label_data: RasaModelData, meta: Dict[Text, Any], data_example: Dict[Text, List[np.ndarray]], model_dir: Text, ): file_name = meta.get("file") tf_model_file = os.path.join(model_dir, file_name + ".tf_model") label_key = LABEL_IDS if meta[INTENT_CLASSIFICATION] else None model_data_example = RasaModelData(label_key=label_key, data=data_example) model = cls.model_class(meta).load( tf_model_file, model_data_example, data_signature=model_data_example.get_signature(), label_data=label_data, index_label_id_mapping=index_label_id_mapping, index_tag_id_mapping=index_tag_id_mapping, config=meta, ) # build the graph for prediction predict_data_example = RasaModelData( label_key=label_key, data={ feature_name: features for feature_name, features in model_data_example.items() if TEXT in feature_name }, ) model.build_for_predict(predict_data_example) return model
def __init__( self, data_signature: Dict[Text, List[FeatureSignature]], label_data: RasaModelData, index_label_id_mapping: Optional[Dict[int, Text]], index_tag_id_mapping: Optional[Dict[int, Text]], config: Dict[Text, Any], ) -> None: super().__init__( name="CRFTransformer", random_seed=config[RANDOM_SEED], tensorboard_log_dir=config[TENSORBOARD_LOG_DIR], tensorboard_log_level=config[TENSORBOARD_LOG_LEVEL], ) self.config = config self.data_signature = data_signature self._check_data() self.predict_data_signature = { feature_name: features for feature_name, features in data_signature.items() if TEXT in feature_name } label_batch = label_data.prepare_batch() self.tf_label_data = self.batch_to_model_data_format( label_batch, label_data.get_signature()) self._num_intents = len(index_label_id_mapping ) if index_label_id_mapping is not None else 0 self._num_tags = len( index_tag_id_mapping) if index_tag_id_mapping is not None else 0 # tf objects, training self._prepare_layers() self._set_optimizer(tf.keras.optimizers.Adam(config[LEARNING_RATE])) self._create_metrics() self._update_metrics_to_log()
def load(cls, path: Union[Text, Path]) -> "TEDPolicy": """Loads a policy from the storage. **Needs to load its featurizer** """ model_path = Path(path) if not model_path.exists(): logger.error( f"Failed to load TED policy model. Path " f"'{model_path.absolute()}' doesn't exist." ) return tf_model_file = model_path / f"{SAVE_MODEL_FILE_NAME}.tf_model" featurizer = TrackerFeaturizer.load(path) if not (model_path / f"{SAVE_MODEL_FILE_NAME}.data_example.pkl").is_file(): return cls(featurizer=featurizer) loaded_data = io_utils.pickle_load( model_path / f"{SAVE_MODEL_FILE_NAME}.data_example.pkl" ) label_data = io_utils.pickle_load( model_path / f"{SAVE_MODEL_FILE_NAME}.label_data.pkl" ) zero_state_features = io_utils.pickle_load( model_path / f"{SAVE_MODEL_FILE_NAME}.zero_state_features.pkl" ) label_data = RasaModelData(data=label_data) meta = io_utils.pickle_load(model_path / f"{SAVE_MODEL_FILE_NAME}.meta.pkl") priority = io_utils.json_unpickle( model_path / f"{SAVE_MODEL_FILE_NAME}.priority.pkl" ) model_data_example = RasaModelData( label_key=LABEL_KEY, label_sub_key=LABEL_SUB_KEY, data=loaded_data ) meta = train_utils.update_similarity_type(meta) model = TED.load( str(tf_model_file), model_data_example, data_signature=model_data_example.get_signature(), config=meta, max_history_tracker_featurizer_used=isinstance( featurizer, MaxHistoryTrackerFeaturizer ), label_data=label_data, ) # build the graph for prediction predict_data_example = RasaModelData( label_key=LABEL_KEY, label_sub_key=LABEL_SUB_KEY, data={ feature_name: features for feature_name, features in model_data_example.items() if feature_name in STATE_LEVEL_FEATURES + FEATURES_TO_ENCODE + [DIALOGUE] }, ) model.build_for_predict(predict_data_example) return cls( featurizer=featurizer, priority=priority, model=model, zero_state_features=zero_state_features, **meta, )