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

Wrong context preperation in SequenceTagger.predict() #3024

Closed
mohammad-al-zoubi opened this issue Dec 13, 2022 · 4 comments
Closed

Wrong context preperation in SequenceTagger.predict() #3024

mohammad-al-zoubi opened this issue Dec 13, 2022 · 4 comments
Labels
fix-for-release-0.12 Must be fixed / completed for release 0.12

Comments

@mohammad-al-zoubi
Copy link

mohammad-al-zoubi commented Dec 13, 2022

Hi everyone,

I finetuned a TransformerWordEmbeddings based on 'microsoft/mdeberta-v3-base' for a NER task and set use_context=True. I noticed some discrepancy between the test that happens automatically after the training (implemented by flair) and the tests that I do with SequenceTagger manually afterwards. After debugging I noticed that the sentences in a batch are being reordered in SequenceTagger.predict():

reordered_sentences = sorted(sentences, key=len, reverse=True)

So the context that is being expanded from this batch is wrong. While the context that is being prepared in the automatic test after training is correct (no sentence reordering happens and SequenceTagger._prepare_tensors(batch) is directly called).

Can you let me know if my observation is correct or if I missed something?

@alanakbik
Copy link
Collaborator

Hello @alzoubi36 thanks for noticing this - it is indeed a big problem that needs to be fixed. The sorting is done to speed-up inference, but when document context needs to be inferred on the fly, the new ordering messes up the contexts. The simplest solution would be to remove the sorting, but this would lead to slow-downs for models that don't require context. We have to think how to best address this.

@helpmefindaname
Copy link
Member

I think a hotfix could be to first call the embedding and then run the predictions, e.g.

model.embeddings.embedd(sentences)
model.predict(sentences)

or to just set the context beforehands:

for first, second in zip(sentences, sentences[1:]):
    first._next_sentence = second
    second._first_sentence = first

but I agree that this is something to be fixed in the library.
I also noticed in the code for adding the context:
There is currently no way to specify that a sentence is the first one and has no left_context, as the check if context is set is self._previous_sentence is not None.

@alanakbik Maybe we can move that logic to the .predict method, such that the embeddings always expect the context to be set already?

@alanakbik
Copy link
Collaborator

@alanakbik Maybe we can move that logic to the .predict method, such that the embeddings always expect the context to be set already?

That would be a solution, however it would mean adding this logic to every predict method in Flair (the SequenceTagger uses a different one from DefaultClassifier) and also this introduces some overhead for models that don't require context (though probably setting the context is not so expensive).

@alanakbik alanakbik added the fix-for-release-0.12 Must be fixed / completed for release 0.12 label Dec 19, 2022
@helpmefindaname
Copy link
Member

This is fixed by the PR above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix-for-release-0.12 Must be fixed / completed for release 0.12
Projects
None yet
Development

No branches or pull requests

3 participants