flairNLP / flair

A very simple framework for state-of-the-art Natural Language Processing (NLP)
https://flairnlp.github.io/flair/
Other
13.98k stars 2.1k forks source link

I am getting different embeddings every time I initialize CharacterEmbeddings #802

Closed n-ibrahimov01 closed 5 years ago

n-ibrahimov01 commented 5 years ago

This is a code that I am using:

from flair.embeddings import CharacterEmbeddings
c_embeddings = CharacterEmbeddings ()

word_string_1 = "Example"
word_string_2 = "Example"

c_embeddings_1 = CharacterEmbeddings ()
c_embeddings_2 = CharacterEmbeddings ()

sentence_embed_it_1 = Sentence (word_string_1)
sentence_embed_it_2 = Sentence (word_string_2)

c_embeddings_1.embed(sentence_embed_it_1)
c_embeddings_1.embed (sentence_embed_it_2)

for token in sentence_embed_it_1:
    emb_1 =  (token.embedding.data)

for token in sentence_embed_it_2:
    emb_2 =  (token.embedding.data)

emb_1 and emb_2 return different values.

alanakbik commented 5 years ago

Hello @n-ibrahimov01 yes the reason for this is that the CharacterEmbeddings are randomly initialized and only make sense if you train them on a downstream task first. So you can use them when training your own model. During model training, these embeddings will then get trained to make sense for that task.

If you're just interested in embedding text and not in training a downstream task model, you should use any of the pre-trained embeddings, such as WordEmbeddings, FlairEmbeddings and BertEmbeddings.

Hellisotherpeople commented 5 years ago

Why don't large pretrained character embeddings models exist yet?

alanakbik commented 5 years ago

@Hellisotherpeople the FlairEmbeddings are large pre-trained character embeddings.

They're different in that FlairEmbeddings are contextualized and pre-trained, whereas CharacterEmbeddings are uncontextualized and require to be trained on a task. We did some comparisons of the two in our COLING 2018 paper; At least on the tasks we looked at, flair embeddings were much better and when we used them, task-trained character features were no longer necessary.

Hellisotherpeople commented 5 years ago

I should have read your paper more closely 😁.

alanakbik commented 5 years ago

:D no worries - will close this issue, but feel free to reopen if you have more questions!