-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Mask vs sequence #5565
Merged
Merged
Mask vs sequence #5565
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8d8f421
merge conflict
evgeniiaraz d4564ec
small changes
evgeniiaraz 18e4b79
small changed by comments
evgeniiaraz e0201f1
None return type
evgeniiaraz d4b7b51
Update docstring
evgeniiaraz ae6dd7b
sequence length as a separate method
evgeniiaraz 2bf5f23
Merge branch 'mask-vs-sequence' of https://github.com/RasaHQ/rasa int…
evgeniiaraz 75ac652
black
evgeniiaraz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,10 +87,10 @@ | |
|
||
TEXT_FEATURES = f"{TEXT}_features" | ||
LABEL_FEATURES = f"{LABEL}_features" | ||
TEXT_MASK = f"{TEXT}_mask" | ||
LABEL_MASK = f"{LABEL}_mask" | ||
LABEL_IDS = f"{LABEL}_ids" | ||
TAG_IDS = "tag_ids" | ||
TEXT_SEQ_LENGTH = f"{TEXT}_lengths" | ||
LABEL_SEQ_LENGTH = f"{LABEL}_lengths" | ||
|
||
|
||
class DIETClassifier(IntentClassifier, EntityExtractor): | ||
|
@@ -484,7 +484,7 @@ def _create_label_data( | |
# to track correctly dynamic sequences | ||
label_data.add_features(LABEL_IDS, [np.expand_dims(label_ids, -1)]) | ||
|
||
label_data.add_mask(LABEL_MASK, LABEL_FEATURES) | ||
label_data.add_lengths(LABEL_SEQ_LENGTH, LABEL_FEATURES) | ||
|
||
return label_data | ||
|
||
|
@@ -558,8 +558,8 @@ def _create_model_data( | |
model_data.add_features(LABEL_IDS, [np.expand_dims(label_ids, -1)]) | ||
model_data.add_features(TAG_IDS, [tag_ids]) | ||
|
||
model_data.add_mask(TEXT_MASK, TEXT_FEATURES) | ||
model_data.add_mask(LABEL_MASK, LABEL_FEATURES) | ||
model_data.add_lengths(TEXT_SEQ_LENGTH, TEXT_FEATURES) | ||
model_data.add_lengths(LABEL_SEQ_LENGTH, LABEL_FEATURES) | ||
|
||
return model_data | ||
|
||
|
@@ -1165,10 +1165,6 @@ def _prepare_entity_recognition_layers(self) -> None: | |
average="micro", | ||
) | ||
|
||
@staticmethod | ||
def _get_sequence_lengths(mask: tf.Tensor) -> tf.Tensor: | ||
return tf.cast(tf.reduce_sum(mask[:, :, 0], axis=1), tf.int32) | ||
|
||
def _combine_sparse_dense_features( | ||
self, | ||
features: List[Union[np.ndarray, tf.Tensor, tf.SparseTensor]], | ||
|
@@ -1255,10 +1251,12 @@ def _create_sequence( | |
|
||
def _create_all_labels(self) -> Tuple[tf.Tensor, tf.Tensor]: | ||
all_label_ids = self.tf_label_data[LABEL_IDS][0] | ||
|
||
label_lengths = tf.cast(self.tf_label_data[LABEL_SEQ_LENGTH][0], dtype=tf.int32) | ||
mask_label = self._compute_mask(label_lengths) | ||
|
||
x = self._create_bow( | ||
self.tf_label_data[LABEL_FEATURES], | ||
self.tf_label_data[LABEL_MASK][0], | ||
self.label_name, | ||
self.tf_label_data[LABEL_FEATURES], mask_label, self.label_name, | ||
) | ||
all_labels_embed = self._tf_layers[f"embed.{LABEL}"](x) | ||
|
||
|
@@ -1352,13 +1350,19 @@ def _calculate_entity_loss( | |
|
||
return loss, f1 | ||
|
||
def _compute_mask(self, sequence_lengths: tf.Tensor) -> tf.Tensor: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be static. |
||
mask = tf.sequence_mask(sequence_lengths, dtype=tf.float32) | ||
# explicitly add last dimension to mask | ||
# to track correctly dynamic sequences | ||
return tf.expand_dims(mask, -1) | ||
tabergma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def batch_loss( | ||
self, batch_in: Union[Tuple[tf.Tensor], Tuple[np.ndarray]] | ||
) -> tf.Tensor: | ||
tf_batch_data = self.batch_to_model_data_format(batch_in, self.data_signature) | ||
|
||
mask_text = tf_batch_data[TEXT_MASK][0] | ||
sequence_lengths = self._get_sequence_lengths(mask_text) | ||
sequence_lengths = tf.cast(tf_batch_data[TEXT_SEQ_LENGTH][0], dtype=tf.int32) | ||
mask_text = self._compute_mask(sequence_lengths) | ||
|
||
( | ||
text_transformed, | ||
|
@@ -1387,11 +1391,12 @@ def batch_loss( | |
# get _cls_ vector for intent classification | ||
cls = self._last_token(text_transformed, sequence_lengths) | ||
|
||
label_lengths = tf.cast(tf_batch_data[LABEL_SEQ_LENGTH][0], dtype=tf.int32) | ||
mask_label = self._compute_mask(label_lengths) | ||
|
||
label_ids = tf_batch_data[LABEL_IDS][0] | ||
label = self._create_bow( | ||
tf_batch_data[LABEL_FEATURES], | ||
tf_batch_data[LABEL_MASK][0], | ||
self.label_name, | ||
tf_batch_data[LABEL_FEATURES], mask_label, self.label_name, | ||
) | ||
loss, acc = self._calculate_label_loss(cls, label, label_ids) | ||
self.intent_loss.update_state(loss) | ||
|
@@ -1417,8 +1422,8 @@ def batch_predict( | |
batch_in, self.predict_data_signature | ||
) | ||
|
||
mask_text = tf_batch_data[TEXT_MASK][0] | ||
sequence_lengths = self._get_sequence_lengths(mask_text) | ||
sequence_lengths = tf.cast(tf_batch_data[TEXT_SEQ_LENGTH][0], dtype=tf.int32) | ||
mask_text = self._compute_mask(sequence_lengths) | ||
|
||
text_transformed, _, _, _ = self._create_sequence( | ||
tf_batch_data[TEXT_FEATURES], mask_text, self.text_name | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a helper method for this? Something like
sequence_length_for
? This logic is repeated all over the place.