Named Entity Recognition NER PyTorch-NLP Python CoNLL-2003 LSTM machine learning natural language processing text classification information retrieval

Deep Dive into Named Entity Recognition with PyTorch-NLP

2023-05-01 11:29:24

//

8 min read

Blog article placeholder

Deep Dive into Named Entity Recognition with PyTorch-NLP.

Named Entity Recognition (NER) is a sub-field of Natural Language Processing (NLP) that involves extracting and classifying entities from unstructured texts. These entities can be anything from people, organizations, locations, products, dates, quantities, and many more. NER is a critical task in many applications, including text classification, sentiment analysis, question answering, and information retrieval. In this post, we will explore the basics of named entity recognition with PyTorch-NLP, a popular Python library for building NLP models with PyTorch.

What is PyTorch-NLP?

PyTorch-NLP is an open-source Python library that provides a suite of tools for building NLP models with PyTorch. It is built on top of PyTorch, a popular machine learning library, and provides a set of pre-trained models and datasets, as well as modules for common NLP tasks, such as tokenization, sequence labeling, and text classification.

Getting Started: Installing PyTorch-NLP

Before we can start building NER models with PyTorch-NLP, we need to install the library. You can do this by running the following command:

``` !pip install pytorch-nlp ```

This will install PyTorch-NLP and all its dependencies. Once the installation is complete, we can import the library in our Python code:

```python import torch import torch.nn as nn import torch.optim as optim from torchnlp.encoders import LabelEncoder, stack_and_pad_tensors from torchnlp.datasets import conll_2003 from torchnlp.metrics import get_entities from tqdm import tqdm ```

NER with PyTorch-NLP

Now that we have PyTorch-NLP installed and imported, we can start building our NER model. In this example, we will use the CoNLL-2003 dataset, which is a standard benchmark dataset for NER. The dataset consists of 4 files: train.txt, test.txt, valid.txt, and labels.txt. The train.txt file contains the training data, while the test.txt file contains the test data. The valid.txt file can be used for validation, and the labels.txt file contains the mapping between the labels and their indices.

We can load the CoNLL-2003 dataset using the following code:

```python train, dev, test = conll_2003() word_encoder = train.get_vocab() label_encoder = LabelEncoder(train.get_labels()) train_iter = stack_and_pad_tensors( [(torch.tensor([word_encoder.encode(word) for word in example['words']])), (label_encoder.encode(example['ner_tags'])) for example in train]) test_iter = stack_and_pad_tensors( [(torch.tensor([word_encoder.encode(word) for word in example['words']])), (label_encoder.encode(example['ner_tags'])) for example in test]) ```

This code loads the train, dev, and test datasets from CoNLL2003 and encodes the words and labels using LabelEncoder and word_encoder, respectively. We also create two iterators for the train and test datasets using stack_and_pad_tensors, which converts the input data into PyTorch tensors and pads the sequences to the maximum length.

Next, we define our NER model using PyTorch. In this example, we use a simple bidirectional LSTM model, which takes the encoded words as input and predicts the corresponding labels:

```python class LSTMModel(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(LSTMModel, self).__init__() self.hidden_size = hidden_size self.embedding = nn.Embedding(input_size, hidden_size) self.lstm = nn.LSTM(hidden_size, hidden_size // 2, num_layers=1, bidirectional=True) self.fc = nn.Linear(hidden_size, output_size) def forward(self, inputs): embeddings = self.embedding(inputs) outputs, _ = self.lstm(embeddings) logits = self.fc(outputs) return logits ```

Finally, we train our model using PyTorch's optimizers and loss functions:

```python input_size = len(word_encoder) output_size = len(label_encoder) hidden_size = 100 batch_size = 32 epochs = 10 model = LSTMModel(input_size, hidden_size, output_size) optimizer = optim.Adam(model.parameters()) criterion = nn.CrossEntropyLoss() for epoch in range(epochs): total_loss = 0 for batch in tqdm(torch.split(train_iter, batch_size)): model.zero_grad() inputs, labels = batch outputs = model(inputs) loss = criterion(outputs.view(-1, output_size), labels.view(-1)) loss.backward() optimizer.step() total_loss += loss.item() print(f'Epoch {epoch+1}, Loss: {total_loss:.4f}') ```

This code trains the LSTM model on the CoNLL-2003 dataset for 10 epochs, using the Adam optimizer and Cross Entropy Loss as the loss function. We split the input data into batches of size 32, and use tqdm to visualize the progress of the training process. After training, we can evaluate our model on the test dataset:

```python test_preds = [] test_targets = [] for batch in tqdm(torch.split(test_iter, batch_size)): inputs, labels = batch outputs = model(inputs) preds = outputs.argmax(dim=-1).tolist() targets = labels.tolist() test_preds += preds test_targets += targets labels = label_encoder.classes entities = get_entities(test_preds, test_targets, labels) ```

This code runs the trained model on the test data, predicts the labels, and compares them with the ground truth. We then use the get_entities function from PyTorch-NLP to extract the entities from the predicted labels.

Conclusion

In this post, we have demonstrated how to build a named entity recognition model with PyTorch-NLP. The example code provides a good starting point for further exploration and customization of NER models using PyTorch.

PyTorch-NLP provides a powerful and convenient toolkit for working with NLP models in PyTorch. With its pre-trained models, datasets, and built-in modules, you can quickly prototype and refine your NER models, and achieve state-of-the-art performance on a variety of benchmark datasets.