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

GH-1041: add support for DistilBERT #1044

Merged
merged 3 commits into from Aug 29, 2019
Merged

Conversation

stefan-it
Copy link
Member

Hi,

this PR adds support for DistilBERT, a distilled version of BERT.

The following example shows how to use it in Flair:

from flair.data import Sentence
from flair.embeddings import BertEmbeddings

embeddings = BertEmbeddings("distilbert-base-uncased")

s = Sentence("Berlin and Munich are nice cities .")
embeddings.embed(s)

for token in s.tokens:
  print(token.embedding)
  print(token.embedding.shape)

Please also refer to this great blog post about distillation 🤗 🤗

I made some experiments on the full CoNLL-2003 corpus. DistilBERT (with this PR-ready version) is only -0.35% behind the BERT (base, uncased) model.

Notice: to try DistilBERT please make sure that you've installed the latest master version of pytorch-transformers (currently, no released version with DistilBERT exists).

@@ -16,6 +16,8 @@
from pytorch_transformers import (
BertTokenizer,
BertModel,
DistilBertTokenizer,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps move these declarations to the if bert_model_or_path.startswith("distilbert"): part of the BertEmbeddings?

This way, it won't trigger an error in Travis when initializing the embeddings classes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :) I changed the import logic. Now an error message is thrown whenever DistilBert* is not found!

@alanakbik
Copy link
Collaborator

Cool, thanks for adding this!

@alanakbik
Copy link
Collaborator

👍

1 similar comment
@yosipk
Copy link
Collaborator

yosipk commented Aug 29, 2019

👍

@yosipk yosipk merged commit fb574e7 into master Aug 29, 2019
@DecentMakeover
Copy link

DecentMakeover commented Aug 30, 2019

@stefan-it Hi ,i wanted to try this out, can i just do a git pull ? , i am currently at

 * (HEAD detached at origin/GH-873-pytorch-transformers)
 master
 origin/master

@stefan-it
Copy link
Member Author

Yes, checkout to master and then a git pull should work :)

@DecentMakeover
Copy link

When trying to run this i get

I have already installed pytorch-transformers and have pulled the latest master code.
Any ideas?

2019-08-30 18:32:04,354 ATTENTION! To use DistilBert, please first install a recent version of pytorch-transformers!
2019-08-30 18:32:04,354 ----------------------------------------------------------------------------------------------------
Traceback (most recent call last):
File "distill_bert.py", line 53, in
BertEmbeddings("distilbert-base-uncased")
File "/media/bubbles/fecf5b15-5a64-477b-8192-f8508a986ffe/ai/flair/flair/embeddings.py", line 1990, in init
self.tokenizer = DistilBertTokenizer.from_pretrained(bert_model_or_path)
UnboundLocalError: local variable 'DistilBertTokenizer' referenced before assignment

@stefan-it
Copy link
Member Author

Try:

$ git clone https://github.com/huggingface/pytorch-transformers.git
$ cd pytorch-transformers
$ pip install -e .

I used that during development :)

@DecentMakeover
Copy link

DecentMakeover commented Aug 30, 2019 via email

@DecentMakeover
Copy link

works like a charm , Thanks.

@alanakbik alanakbik deleted the GH-1041-distilbert-support branch September 4, 2019 11:41
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.

None yet

4 participants