Views
No views yet
1
2from transformers import AutoConfig, AutoTokenizer, AutoModelForTokenClassification
3tokenizer = AutoTokenizer.from_pretrained('Kashob/SciBERTNER')
4model = AutoModelForTokenClassification.from_pretrained('Kashob/SciBERTNER')
5config = AutoConfig.from_pretrained('Kashob/SciBERTNER')
6id2tag = config.id2label
7
8text = 'The paper tackles the problem of endowing Transformers with the ability to encode information about the past via recurrence. The proposed architecture can leverage the recurrent connections to improve the sample efficiency while maintaining expressivity due to the use of self-attention.'.split()
9
10inputs = tokenizer(text, is_split_into_words=True, return_tensors="pt")
11with torch.no_grad():
12 outputs = model(**inputs)
13 predictions = outputs.logits.argmax(-1)
14
15tokenized_text = tokenizer.convert_ids_to_tokens(inputs['input_ids'].tolist()[0])
16predicted_labels = [id2tag[label_id] for label_id in predictions[0].tolist()]
17print(tokenized_text)
18print(predicted_labels)
19
20Output:
21['[CLS]', 'the', 'paper', 'tackle', '##s', 'the', 'problem', 'of', 'endow', '##ing', 'transformers', 'with', 'the', 'ability', 'to', 'encode', 'information', 'about', 'the', 'past', 'via', 'recurrence', '.', 'the', 'proposed', 'architecture', 'can', 'leverage', 'the', 'recurrent', 'connections', 'to', 'improve', 'the', 'sample', 'efficiency', 'while', 'maintaining', 'express', '##ivity', 'due', 'to', 'the', 'use', 'of', 'self', '-', 'attention', '.', '[SEP]']
22['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-OtherScientificTerm', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-Method', 'O', 'O', 'O', 'B-Generic', 'O', 'O', 'O', 'B-OtherScientificTerm', 'I-OtherScientificTerm', 'O', 'O', 'O', 'B-Metric', 'I-Metric', 'O', 'O', 'B-Metric', 'I-OtherScientificTerm', 'O', 'O', 'O', 'O', 'O', 'B-Method', 'I-OtherScientificTerm', 'I-OtherScientificTerm', 'O', 'O']
23