-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Optimize RelationClassifier by adding the option to filter long sentences and truncate context #3593
base: master
Are you sure you want to change the base?
Conversation
max_allowed_tokens_between_entities: int = 20, | ||
max_surrounding_context_length: int = 10, |
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.
Maybe if would make sense to also allow for None
values to disable the measures. ̀This could then also be used as default value for _init_model_with_state_dict
to not change the behaviour of existing models.
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.
Good idea!
max_allowed_tokens_between_entities=state.get("max_allowed_tokens_between_entities", 25), | ||
max_surrounding_context_length=state.get("max_surrounding_context_length", 50), |
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.
The default parameters for backwards compatibility are different to the ones in the ̀ init` method. Are these a better fit?
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.
backwards compatibility is tricky, since older models will have no limitations on max allowed tokens or surrounding context. It's probably best to set really high numbers here (e.g., even higher).
head_idx = -10000 | ||
tail_idx = 10000 |
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.
For safety and as a sanity check, we could also initialize these values as None
and have an assertion after the for token in original_sentence
loop that these variables should not be None
.
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.
I had that before, but that gave me mypy errors. But I agree that this is better.
…ax operations instead of if-statements
…urrounding_context_length` filter for backwards compatibility
…rounding_context_length` parameters
I've incorporated the suggestions and also added a test case for the new parameters. |
The
RelationClassifier
separately considers each pair of entities in aSentence
for relation classification. If a sentence is long and contains many entities, this leads to a very large number of forward passes.This PR introduces two new parameters to RelationClassifier to optimize this behavior:
max_allowed_tokens_between_entities
allows users to specify the maximum allowed distance between two entities in order to be considered a relation candidate. The idea is that entities that lie far apart in a text are unlikely to be in relation. All entity pairs with too many tokens in between are filtered.max_surrounding_context_length
allows users to specify how much additional context (text around the two entities) is used to make the relation classification. Setting this to a low value makes classification more computationally efficient, since smaller encoded sentences are used in the forward pass.Further, the PR introduces a new sanity check to ensure that one entity in a pair does not contain the other.
To enable this, the
_encode_sentence
function is changed to possibly return None if one of the criteria is not met. The_encode_sentence_for_inference
and_encode_sentence_for_training
methods are accordingly changed to check for None values before the yield.