Here’s an example:
# Import necessary libraries
from transformers import AutoTokenizer, DistilBertForSequenceClassification
# Define the name of the pre-trained model we want to use
model_name = "distilbert-base-uncased"
# Load in the tokenizer for our chosen model
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Load in the actual model itself
model = DistilBertForSequenceClassification.from_pretrained(model_name)
# Let's say we have some text that we want to classify: "Hello, my dog is cute"
inputs = tokenizer("Hello, my dog is cute", return_tensors="pt") # Convert our input string into a format that can be used by the model (in this case, PyTorch tensors)
# Run the forward pass of the model and return the output in logit form (i.e., unscaled probabilities)
with torch.no_grad():
logits = model(**inputs).logits
# Find the index with the highest probability (i.e., the predicted class ID)
predicted_class_id = logits.argmax().item()
So, what’s going on here? First we load in our chosen pre-trained model and its associated tokenizer using `AutoTokenizer` and `DistilBertForSequenceClassification`, respectively. Then we create an input string that we want to classify (in this case, “Hello, my dog is cute”). We convert the input string into a format that can be used by the model using `tokenizer()`. Finally, we run the forward pass of the model and get back some output in logit form. This output represents the probability of each possible label for our input text (i.e., “Hello, my dog is cute” could potentially belong to any number of different categories). We then find the index with the highest probability using `argmax()` and convert it into an integer using `item()`.
Using DistilBertForSequenceClassification for Single-Label Classification. It’s not rocket science, but it can be pretty useful if you need to classify text based on some predefined set of categories (like “positive” or “negative”).