flairNLP / flair

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

AttributeError: 'BertTokenizer' object has no attribute 'unique_no_split_tokens' #1796

Closed achang97 closed 3 years ago

achang97 commented 4 years ago

Describe the bug When attempting to use Sequence Tagger with keyphrase as the argument, I get the following error:

AttributeError: 'BertTokenizer' object has no attribute 'unique_no_split_tokens'

To Reproduce

  sentences = [Sentence(sent, use_tokenizer=True) for sent in split_single(text)]

  # run KEYPHRASE over sentence
  tagger.predict(sentences) # Errors out here

Expected behavior I expect no error and for the key phrases to be detected. I followed the example in the release notes:

# load keyphrase tagger
tagger = SequenceTagger.load('keyphrase')

# load communicative function tagger
sentence = Sentence("Here, we describe the engineering of a new class of ECHs through the "
                    "functionalization of non-conductive polymers with a conductive choline-based "
                    "bio-ionic liquid (Bio-IL).", use_tokenizer=True)

# predict and print labels
tagger.predict(sentence)
print(sentence)

Screenshots If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

Additional context Add any other context about the problem here.

whoisjones commented 4 years ago

@achang97 we're currently looking into this. at first sight code looks fine, could be due to the fact that our model has been trained on an older transformer version. we'll check and keep you updated.

achang97 commented 4 years ago

Thanks, please let me know!

whoisjones commented 4 years ago

@achang97 The error throws in a lib we're using for Transformer models - I was not sure how this error came up but it might be due to a new release from their side. Thus we trained some new models (thought that might fix the issue of loading the model correctly) - unfortunately i get new errors, similar to the one above but a different one. So update on this here: we're still investigating.

vinhkhuc commented 4 years ago

I got the same error with flair 0.6. Downgrading transformers to 2.11.0 seems to fix it.

alanakbik commented 4 years ago

We found this error only occurs if training with torch 1.5.1 - is this the same for you?

vinhkhuc commented 4 years ago

Actually, no. I didn't get any issue with torch 1.5.1:

import torch
import transformers
from flair.data import Sentence
from flair.models import SequenceTagger

print(f"Torch's version: {torch.__version__}")
print(f"Transformers' version: {transformers.__version__}")

print("Loading keyphrase model ...")
keyphrase_model = SequenceTagger.load("keyphrase")

text = 'Wisconsin residents receiving an extra $300 in weekly unemployment benefits are being asked to pay back the money if Congress enacts new legislation to replace the jobless aid created by President Trump\'s executive action last month.'
sentence = Sentence(text)
keyphrase_model.predict(sentence)
print(sentence)

for kp in sentence.get_spans():
    print(kp)
Torch's version: 1.5.1
Transformers' version: 2.11.0
Loading keyphrase model ...
2020-09-21 12:30:16,885 loading file /Users/khuc/.flair/models/keyphrase-en-scibert.pt
Sentence: "Wisconsin residents receiving an extra $ 300 in weekly unemployment benefits are being asked to pay back the money if Congress enacts new legislation to replace the jobless aid created by President Trump 's executive action last month ."   [− Tokens: 39  − Token-Labels: "Wisconsin <B-Material> residents <E-Material> receiving an extra $ 300 in weekly unemployment benefits are being asked to pay <B-Process> back <I-Process> the <I-Process> money <E-Process> if Congress enacts new legislation to replace the jobless aid created by President Trump 's executive action last month ."]
Span [1,2]: "Wisconsin residents"   [− Labels: Material (0.8445)]
Span [16,17,18,19]: "pay back the money"   [− Labels: Process (0.6335)]
alanakbik commented 4 years ago

Ah ok thanks for the info! I think we need to re-train the model with torch 1.6.0 to work across all transformer versions.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

JieShenAI commented 1 year ago

pip install transformers==3.4.0

新版本不支持,安装旧版本就解决了这个问题