9951 explained code solutions for 126 technologies


python-pytorchHow can I use BERT and PyTorch together to develop software?


BERT (Bidirectional Encoder Representations from Transformers) is a powerful natural language processing (NLP) model developed by Google that can be used for a variety of tasks, such as text classification, question answering, and language understanding. PyTorch is an open-source machine learning library for Python that provides a wide range of algorithms for deep learning. Combining BERT and PyTorch can be used to develop software for a variety of NLP tasks.

Below is an example of how to use BERT and PyTorch together to classify text:

import torch
from transformers import BertTokenizer, BertForSequenceClassification

# Load the BERT tokenizer.
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

# Tokenize input
text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]"
tokenized_text = tokenizer.tokenize(text)

# Convert token to vocabulary indices
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)

# Define the sequence
segments_ids = [1] * len(tokenized_text)

# Convert inputs to PyTorch tensors
tokens_tensor = torch.tensor([indexed_tokens])
segments_tensors = torch.tensor([segments_ids])

# Load pre-trained model (weights)
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')

# Predict the class (output)
# Output shape = (batch_size, num_labels)
# num_labels = 2 (for binary classification)
# batch_size = 1
output = model(tokens_tensor, token_type_ids=segments_tensors)
print(output[0])

# Output
tensor([[-2.9201,  2.8297]], grad_fn=<AddmmBackward>)

In the example code above:

  1. The BERT tokenizer is loaded and used to tokenize the input text.
  2. The tokenized text is converted to vocabulary indices.
  3. The segments_ids are defined.
  4. The input is converted to PyTorch tensors.
  5. The pre-trained BERT model is loaded.
  6. The output is predicted using the model.

Helpful links

Edit this code on GitHub