Views
No views yet
allenai/scibert_scivocab_cased has been finetuned for Named Entity
Recognition (NER) dowstream task. The code to train the NER can be found here.| Class Label | # training examples | # evaluation examples |
|---|---|---|
| O | 382,963 | 81,647 |
| B-protein | 30,269 | 5,067 |
| I-protein | 24,848 | 4,774 |
| B-cell_type | 6,718 | 1,921 |
| I-cell_type | 8,748 | 2,991 |
| B-DNA | 9,533 | 1,056 |
| I-DNA | 15,774 | 1,789 |
| B-cell_line | 3,830 | 500 |
| I-cell_line | 7,387 | 9,89 |
| B-RNA | 951 | 118 |
| I-RNA | 1,530 | 187 |
| precision | recall | f1-score | |
|---|---|---|---|
| cell_line | 0.5205 | 0.7100 | 0.6007 |
| cell_type | 0.7736 | 0.7422 | 0.7576 |
| protein | 0.6953 | 0.8459 | 0.7633 |
| DNA | 0.6997 | 0.7894 | 0.7419 |
| RNA | 0.6985 | 0.8051 | 0.7480 |
| micro avg | 0.6984 | 0.8076 | 0.7490 |
| macro avg | 0.7032 | 0.8076 | 0.7498 |
1from transformers import pipeline
2
3text = "Mouse thymus was used as a source of glucocorticoid receptor from normal CS lymphocytes."
4
5nlp_ner = pipeline("ner",
6 model='fran-martinez/scibert_scivocab_cased_ner_jnlpba',
7 tokenizer='fran-martinez/scibert_scivocab_cased_ner_jnlpba')
8
9nlp_ner(text)
10
11"""
12Output:
13---------------------------
14[
15{'word': 'glucocorticoid',
16'score': 0.9894881248474121,
17'entity': 'B-protein'},
18
19{'word': 'receptor',
20'score': 0.989505410194397,
21'entity': 'I-protein'},
22
23{'word': 'normal',
24'score': 0.7680378556251526,
25'entity': 'B-cell_type'},
26
27{'word': 'cs',
28'score': 0.5176806449890137,
29'entity': 'I-cell_type'},
30
31{'word': 'lymphocytes',
32'score': 0.9898491501808167,
33'entity': 'I-cell_type'}
34]
35"""1import torch
2from transformers import AutoTokenizer, AutoModelForTokenClassification
3
4# Example
5text = "Mouse thymus was used as a source of glucocorticoid receptor from normal CS lymphocytes."
6
7# Load model
8tokenizer = AutoTokenizer.from_pretrained("fran-martinez/scibert_scivocab_cased_ner_jnlpba")
9model = AutoModelForTokenClassification.from_pretrained("fran-martinez/scibert_scivocab_cased_ner_jnlpba")
10
11# Get input for BERT
12input_ids = torch.tensor(tokenizer.encode(text)).unsqueeze(0)
13
14# Predict
15with torch.no_grad():
16 outputs = model(input_ids)
17
18# From the output let's take the first element of the tuple.
19# Then, let's get rid of [CLS] and [SEP] tokens (first and last)
20predictions = outputs[0].argmax(axis=-1)[0][1:-1]
21
22# Map label class indexes to string labels.
23for token, pred in zip(tokenizer.tokenize(text), predictions):
24 print(token, '->', model.config.id2label[pred.numpy().item()])
25
26"""
27Output:
28---------------------------
29mouse -> O
30thymus -> O
31was -> O
32used -> O
33as -> O
34a -> O
35source -> O
36of -> O
37glucocorticoid -> B-protein
38receptor -> I-protein
39from -> O
40normal -> B-cell_type
41cs -> I-cell_type
42lymphocytes -> I-cell_type
43. -> O
44"""