NER
Use this guided lab to apply the lesson concepts.
Task
In this lab, you need to train named entity recognition model for medical terms.
The Dataset
To train NER model, we need properly labeled dataset with medical entities. BC5CDR dataset contains labeled diseases and chemicals entities from more than 1500 papers. You may download the dataset after registering at their web site.
BC5CDR Dataset looks like this:
6794356|t|Tricuspid valve regurgitation and lithium carbonate toxicity in a newborn infant.
6794356|a|A newborn with massive tricuspid regurgitation, atrial flutter, congestive heart failure, and a high serum lithium level is described. This is the first patient to initially manifest tricuspid regurgitation and atrial flutter, and the 11th described patient with cardiac disease among infants exposed to lithium compounds in the first trimester of pregnancy. Sixty-three percent of these infants had tricuspid valve involvement. Lithium carbonate may be a factor in the increasing incidence of congenital heart disease when taken during early pregnancy. It also causes neurologic depression, cyanosis, and cardiac arrhythmia when consumed prior to delivery.
6794356 0 29 Tricuspid valve regurgitation Disease D014262
6794356 34 51 lithium carbonate Chemical D016651
6794356 52 60 toxicity Disease D064420
...
In this dataset, there are paper title and abstract in the first two lines, and then there are individual entities, with beginning and end positions within title+abstract block. In addition to entity type, you get the ontology ID of this entity within some medical ontology.
You will need to write some Python code to convert this into BIO encoding.
The Network
Build an LSTM baseline from the lesson, then compare it with a pre-trained transformer. A fair comparison depends on the split, entity-level metric, and compute budget; no model family is guaranteed to win in every setting.
Microsoft Research's former PubMedBERT checkpoint is now named BiomedBERT. Its model card states that it was pre-trained from scratch on PubMed abstracts. Adapt it for token classification with the current Auto classes:
from transformers import AutoModelForTokenClassification, AutoTokenizer
model_name
Align BIO labels with subword tokens, and report entity-level precision, recall, and F1 on a held-out split. Record the model identifier and revision plus the dataset version so the experiment can be reproduced.
Takeaway
NER can help organize biomedical literature, but it does not establish causality or provide a diagnosis. Review errors with a domain expert before any sensitive use, and never send patient text or private health data to an external service.