NLP with Transformers: Fundamentals and Core ApplicationsChapter 99
9. Step 6: Using the Model for Prediction
Section 9 of 10-~ 12 min read-Synced from Cuantum content
Finally, we’ll use the trained model to predict sentiment for new reviews.
Code Example: Predicting Sentiment
# New reviews for predictionreviews = [ "The movie was absolutely fantastic! A must-watch.", "I regret watching this film. It was a waste of time.", "The movie was just okay, nothing special."] # Tokenize new reviewsinputs = tokenizer(reviews, truncation=True, padding=True, return_tensors="pt") # Get predictionsoutputs = model(**inputs)predictions = outputs.logits.argmax(dim=-1) # Map predictions to labelslabels = ["Negative", "Positive"]for review, prediction in zip(reviews, predictions): print(f"Review: {review}") print(f"Predicted Sentiment: {labels[prediction]}")