NLP with Transformers: Advanced Techniques and Multimodal ApplicationsChapter 93
Step 2: Load and Preprocess the Dataset
Section 4 of 9-~ 12 min read-Synced from Cuantum content
Use the Hugging Face datasets library to load and preprocess an NER dataset. This library provides convenient tools for working with machine learning datasets and includes built-in support for popular NER datasets like CoNLL-2003. The library handles data loading, caching, and preprocessing automatically, making it easier to focus on model development. It also provides methods for data validation, filtering, and transformation that are essential for preparing NER training data. The preprocessing steps typically include tokenization, label alignment, and converting the data into the required format for model training.
from datasets import load_dataset # Load CoNLL-2003 datasetdataset = load_dataset("conll2003") # Example: Inspect the datasetprint(dataset["train"][0])Lets breakdown this code:
- First, we import the load_dataset function from the Hugging Face datasets library:
from datasets import load_dataset- Then we load the CoNLL-2003 dataset, which is a standard dataset for NER tasks. This dataset contains annotated text with four types of entities:
- - Persons (PER)
- Locations (LOC)
- Organizations (ORG)
- Miscellaneous entities (MISC)
- The code prints an example from the training set, which shows the format of the data:
- - "tokens": Contains the individual words in the text
- "ner_tags": Contains corresponding numeric labels that identify the entity type for each token
Output Example:
{ "tokens": ["EU", "rejects", "German", "call", "to", "boycott", "British", "lamb", "."], "ner_tags": [3, 0, 1, 0, 0, 0, 1, 0, 0, 0]}