Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get ML components ready for incremental training #7419

Merged
merged 57 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
136628d
doc strings and changes needed to cvf
dakshvar22 Nov 30, 2020
b709132
added tests, small refactoring in cvf
dakshvar22 Dec 1, 2020
cad6fa6
refactor regex featurizers and fix tests
dakshvar22 Dec 2, 2020
4590b85
added tests for regex featurizer, comments and doc strings
dakshvar22 Dec 3, 2020
d3bf725
rename 'finetune_mode' parameter inside load
dakshvar22 Dec 3, 2020
7e63d14
Merge branch 'continuous_training' into iss-7413
dakshvar22 Dec 3, 2020
a8555c5
address review comments, make ML components inside NLU loadable in fi…
dakshvar22 Dec 3, 2020
e83318f
Merge branch 'continuous_training' into iss-7413
dakshvar22 Dec 3, 2020
969750a
try resetting default additional slots in cvf to 0, see if results go…
dakshvar22 Dec 3, 2020
3d469c8
revert default in regex also, to see if model regression tests pass
dakshvar22 Dec 4, 2020
e464dfe
rectify how regex featurizer is loaded
dakshvar22 Dec 4, 2020
d5fdb41
revert back defaults for additional vocab params in cvf and regex
dakshvar22 Dec 4, 2020
a868b0e
add default minimum for cvf as well
dakshvar22 Dec 4, 2020
eff925f
Load core model in fine-tuning mode
joejuzl Dec 4, 2020
92526a2
Core finetune loading test
joejuzl Dec 7, 2020
3be4ea9
Test and PR comments
joejuzl Dec 7, 2020
5e8eeb4
Fallback to default epochs
joejuzl Dec 7, 2020
ab2058d
Test policy and ensemble fine-tuning exception cases
joejuzl Dec 7, 2020
cbab2d0
Remove epoch_override from Policy.load
joejuzl Dec 7, 2020
a3d8f6d
merge base branch;
dakshvar22 Dec 7, 2020
364c31c
Apply suggestions from code review
dakshvar22 Dec 7, 2020
c71fcd8
review comments and add tests for loaded diet and rs
dakshvar22 Dec 7, 2020
fd68f4d
merge review commits
dakshvar22 Dec 7, 2020
1f9f42c
Merge branch 'continuous_training' into iss-7413
dakshvar22 Dec 8, 2020
95a3a54
fix regex tests
dakshvar22 Dec 8, 2020
3b4ccc0
use kwargs
joejuzl Dec 8, 2020
da77d16
Merge remote-tracking branch 'origin/continuous_training' into 7329/l…
joejuzl Dec 8, 2020
5dc82b7
fix
joejuzl Dec 8, 2020
311ea47
fix train tests
joejuzl Dec 8, 2020
0bd119c
More test fixes
joejuzl Dec 8, 2020
3cb91b6
Apply suggestions from code review
joejuzl Dec 8, 2020
73132f0
remove unneeded sklearn epochs
joejuzl Dec 8, 2020
28d5fe9
Apply suggestions from code review
joejuzl Dec 8, 2020
0e6ddba
PR comments for warning strings
joejuzl Dec 8, 2020
14a6a31
Add typing
joejuzl Dec 8, 2020
61aefc3
add back invalid model tests
joejuzl Dec 8, 2020
d1e0295
handle empty sections in config
joejuzl Dec 8, 2020
9ebe706
review comments
dakshvar22 Dec 8, 2020
13d160e
Merge branch '7329/load_models_in_finetune_mode_core' into iss-7413
dakshvar22 Dec 8, 2020
527a44e
Merge branch 'fix_fingerprint_for_empty_pipeline_or_policy' into iss-…
dakshvar22 Dec 8, 2020
7c10b09
make core models finetunable
dakshvar22 Dec 8, 2020
bd7927f
add tests finetuning core policies
dakshvar22 Dec 8, 2020
64b681e
add print for loaded model
dakshvar22 Dec 9, 2020
cf4aaf6
add vocabulary stats logging for cvf
dakshvar22 Dec 9, 2020
b2c8249
merge base branch
dakshvar22 Dec 9, 2020
757174d
code quality
dakshvar22 Dec 9, 2020
68b76df
review comments
dakshvar22 Dec 9, 2020
6a2d12e
reduce number of finetuning epochs in tests
dakshvar22 Dec 9, 2020
90b5394
Use fingerprinting for finetuning and add more tests
joejuzl Dec 9, 2020
5721fe6
review comments
dakshvar22 Dec 9, 2020
b2c2b4f
Merge branch 'use_finetuning_fingerprinting' into iss-7413
dakshvar22 Dec 9, 2020
0f83903
review comments
dakshvar22 Dec 9, 2020
4c2d249
fix tests
dakshvar22 Dec 9, 2020
6e0c9b9
Use all training labels for fingerprinting
joejuzl Dec 10, 2020
dd7135b
rename to action_names
joejuzl Dec 11, 2020
b4a88f2
fix log in cvf and merge new changes for fingerprinting
dakshvar22 Dec 11, 2020
98f57ec
merge basse branch
dakshvar22 Dec 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rasa/core/policies/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ def __init__(
self,
featurizer: Optional[TrackerFeaturizer] = None,
priority: int = DEFAULT_POLICY_PRIORITY,
should_finetune: bool = False,
**kwargs: Any,
) -> None:
"""Constructs a new Policy object."""
self.__featurizer = self._create_featurizer(featurizer)
self.priority = priority
self.finetune_mode = should_finetune

@property
def featurizer(self):
Expand Down
61 changes: 38 additions & 23 deletions rasa/core/policies/ted_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,16 @@ def __init__(
max_history: Optional[int] = None,
model: Optional[RasaModel] = None,
zero_state_features: Optional[Dict[Text, List["Features"]]] = None,
should_finetune: bool = False,
**kwargs: Any,
) -> None:
"""Declare instance variables with default values."""
if not featurizer:
featurizer = self._standard_featurizer(max_history)

super().__init__(featurizer, priority, **kwargs)
super().__init__(
featurizer, priority, should_finetune=should_finetune, **kwargs
)
if isinstance(featurizer, FullDialogueTrackerFeaturizer):
self.is_full_dialogue_featurizer_used = True
else:
Expand Down Expand Up @@ -349,12 +352,16 @@ def train(
# keep one example for persisting and loading
self.data_example = model_data.first_data_example()

self.model = TED(
model_data.get_signature(),
self.config,
isinstance(self.featurizer, MaxHistoryTrackerFeaturizer),
self._label_data,
)
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 = TED(
model_data.get_signature(),
self.config,
isinstance(self.featurizer, MaxHistoryTrackerFeaturizer),
self._label_data,
)

self.model.fit(
model_data,
Expand Down Expand Up @@ -436,7 +443,13 @@ def persist(self, path: Union[Text, Path]) -> None:
)

@classmethod
def load(cls, path: Union[Text, Path], **kwargs: Any) -> "TEDPolicy":
def load(
cls,
path: Union[Text, Path],
should_finetune: bool = False,
epoch_override: int = defaults[EPOCHS],
**kwargs: Any,
) -> "TEDPolicy":
"""Loads a policy from the storage.

**Needs to load its featurizer**
Expand Down Expand Up @@ -476,6 +489,8 @@ def load(cls, path: Union[Text, Path], **kwargs: Any) -> "TEDPolicy":
)
meta = train_utils.update_similarity_type(meta)

meta[EPOCHS] = epoch_override

model = TED.load(
str(tf_model_file),
model_data_example,
Expand All @@ -485,30 +500,30 @@ def load(cls, path: Union[Text, Path], **kwargs: Any) -> "TEDPolicy":
featurizer, MaxHistoryTrackerFeaturizer
),
label_data=label_data,
finetune_mode=should_finetune,
)

# 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)
if not should_finetune:
# build the graph for prediction

meta["should_finetune"] = kwargs.get("should_finetune", False)
if "epoch_override" in kwargs:
meta[EPOCHS] = kwargs["epoch_override"]
features_to_select = STATE_LEVEL_FEATURES + FEATURES_TO_ENCODE + [DIALOGUE]
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 features_to_select
},
)
model.build_for_predict(predict_data_example)

return cls(
featurizer=featurizer,
priority=priority,
model=model,
zero_state_features=zero_state_features,
should_finetune=should_finetune,
**meta,
)

Expand Down
59 changes: 44 additions & 15 deletions rasa/nlu/classifiers/diet_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ def __init__(
index_label_id_mapping: Optional[Dict[int, Text]] = None,
entity_tag_specs: Optional[List[EntityTagSpec]] = None,
model: Optional[RasaModel] = None,
finetune_mode: bool = False,
) -> None:
"""Declare instance variables with default values."""

if component_config is not None and EPOCHS not in component_config:
rasa.shared.utils.io.raise_warning(
f"Please configure the number of '{EPOCHS}' in your configuration file."
Expand All @@ -333,6 +333,17 @@ def __init__(

self.split_entities_config = self.init_split_entities()

self.finetune_mode = finetune_mode

if not self.model and self.finetune_mode:
raise rasa.shared.exceptions.InvalidParameterException(
f"{self.__class__.__name__} was instantiated "
f"with `model=None` and `finetune_mode=True`. "
f"This is not a valid combination as the component "
f"needs an already instantiated and trained model "
f"to continue training in finetune mode."
)

@property
def label_key(self) -> Optional[Text]:
"""Return key if intent classification is activated."""
Expand Down Expand Up @@ -766,7 +777,9 @@ def train(
# keep one example for persisting and loading
self._data_example = model_data.first_data_example()

self.model = self._instantiate_model_class(model_data)
if not self.finetune_mode:
# No pre-trained model to load from. Create a new instance of the model.
self.model = self._instantiate_model_class(model_data)

self.model.fit(
model_data,
Expand Down Expand Up @@ -956,10 +969,10 @@ def load(
model_dir: Text = None,
model_metadata: Metadata = None,
cached_component: Optional["DIETClassifier"] = None,
should_finetune: bool = False,
**kwargs: Any,
) -> "DIETClassifier":
"""Loads the trained model from the provided directory."""

if not model_dir or not meta.get("file"):
logger.debug(
f"Failed to load model for '{cls.__name__}'. "
Expand All @@ -979,14 +992,20 @@ def load(
meta = train_utils.update_similarity_type(meta)

model = cls._load_model(
entity_tag_specs, label_data, meta, data_example, model_dir
entity_tag_specs,
label_data,
meta,
data_example,
model_dir,
finetune_mode=should_finetune,
)

return cls(
component_config=meta,
index_label_id_mapping=index_label_id_mapping,
entity_tag_specs=entity_tag_specs,
model=model,
finetune_mode=should_finetune,
)

@classmethod
Expand Down Expand Up @@ -1039,6 +1058,7 @@ def _load_model(
meta: Dict[Text, Any],
data_example: Dict[Text, Dict[Text, List[np.ndarray]]],
model_dir: Text,
finetune_mode: bool = False,
) -> "RasaModel":
file_name = meta.get("file")
tf_model_file = os.path.join(model_dir, file_name + ".tf_model")
Expand All @@ -1051,20 +1071,27 @@ def _load_model(
)

model = cls._load_model_class(
tf_model_file, model_data_example, label_data, entity_tag_specs, meta
tf_model_file,
model_data_example,
label_data,
entity_tag_specs,
meta,
finetune_mode=finetune_mode,
)

# 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
},
)
if not finetune_mode:

# 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)
model.build_for_predict(predict_data_example)

return model

Expand All @@ -1076,6 +1103,7 @@ def _load_model_class(
label_data: RasaModelData,
entity_tag_specs: List[EntityTagSpec],
meta: Dict[Text, Any],
finetune_mode: bool,
) -> "RasaModel":

return cls.model_class().load(
dakshvar22 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -1085,6 +1113,7 @@ def _load_model_class(
label_data=label_data,
entity_tag_specs=entity_tag_specs,
config=copy.deepcopy(meta),
finetune_mode=finetune_mode,
)

def _instantiate_model_class(self, model_data: RasaModelData) -> "RasaModel":
Expand Down
2 changes: 2 additions & 0 deletions rasa/nlu/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@
FEATURIZER_CLASS_ALIAS = "alias"

NO_LENGTH_RESTRICTION = -1
MIN_ADDITIONAL_REGEX_PATTERNS = 10
MIN_ADDITIONAL_CVF_VOCABULARY = 1000
Loading