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

Add pretrained relation extraction models #2492

Merged
merged 2 commits into from
Oct 30, 2021
Merged

Add pretrained relation extraction models #2492

merged 2 commits into from
Oct 30, 2021

Conversation

alanakbik
Copy link
Collaborator

This PR adds a few relation extraction models, trained over a modified version of TACRED. Two models are added: relations and relations-fast.

To use these models, you also need an entity tagger. The tagger identifies entities, then the relation extractor possible entities. For instance use this code:

from flair.data import Sentence
from flair.models import RelationExtractor, SequenceTagger

# 1. make example sentence
sentence = Sentence("George was born in Washington")

# 2. load entity tagger and predict entities
tagger = SequenceTagger.load('ner-fast')
tagger.predict(sentence)

# check which entities have been found in the sentence
entities = sentence.get_labels('ner')
for entity in entities:
    print(entity)

# 3. load relation extractor
extractor: RelationExtractor = RelationExtractor.load('relations-fast')

# predict relations
extractor.predict(sentence)

# check which relations have been found
relations = sentence.get_labels('relation')
for relation in relations:
    print(relation)

@alanakbik alanakbik merged commit 46569a6 into master Oct 30, 2021
@alanakbik alanakbik deleted the relation-models branch October 30, 2021 12:57
@alejandrojcastaneira
Copy link

Thank you very much for adding this, can you provide some insights or a short description of the architecture of the relations extraction models?

@alanakbik
Copy link
Collaborator Author

Hello @alejandrojcastaneira it we used a very simple architecture here: the RelationExtractor goes through all pairwise combinations of entities found in a sentence. For each entity pair, it takes the embeddings of both entities and concatenates them. This concatenated entity pair embedding is then classified.

@SohaK
Copy link

SohaK commented Mar 22, 2022

@alanakbik does the embedding include some sort of positional embedding? I am asking since I have a use-case that for example in a sentence there are two persons and two birth_places. Can this Relation Extraction model be trained to distinguish between these two birth_place relations based on context of the sentence?

@djstrong
Copy link
Contributor

Yes if you use contextual embeddings.

@SohaK
Copy link

SohaK commented Mar 22, 2022

@alanakbik @djstrong in my question I meant positional context not semantic context. Is there any paper or blog that shed some light to the FLAIR Relation Extraction?

@djstrong
Copy link
Contributor

Semantic context is usually positional. Flair RE is extracting embeddings of the spans, concats them and pass through fully connected neural network.

@SohaK
Copy link

SohaK commented Mar 22, 2022

@djstrong is the 'embedding' the dynamic embedding of the span in the current sentence, or the pretrained embedding?

@djstrong
Copy link
Contributor

You can choose embeddings (dynamic or static).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants