Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconNLP with Transformers: Fundamentals and Core Applications
NLP with Transformers: Fundamentals and Core Applications

Project 1: Sentiment Analysis with BERT

9. Step 6: Using the Model for Prediction

Finally, we’ll use the trained model to predict sentiment for new reviews.

Code Example: Predicting Sentiment

# New reviews for prediction
reviews = [
    "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 reviews
inputs = tokenizer(reviews, truncation=True, padding=True, return_tensors="pt")

# Get predictions
outputs = model(**inputs)
predictions = outputs.logits.argmax(dim=-1)

# Map predictions to labels
labels = ["Negative", "Positive"]
for review, prediction in zip(reviews, predictions):
    print(f"Review: {review}")
    print(f"Predicted Sentiment: {labels[prediction]}")

9. Step 6: Using the Model for Prediction

Finally, we’ll use the trained model to predict sentiment for new reviews.

Code Example: Predicting Sentiment

# New reviews for prediction
reviews = [
    "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 reviews
inputs = tokenizer(reviews, truncation=True, padding=True, return_tensors="pt")

# Get predictions
outputs = model(**inputs)
predictions = outputs.logits.argmax(dim=-1)

# Map predictions to labels
labels = ["Negative", "Positive"]
for review, prediction in zip(reviews, predictions):
    print(f"Review: {review}")
    print(f"Predicted Sentiment: {labels[prediction]}")

9. Step 6: Using the Model for Prediction

Finally, we’ll use the trained model to predict sentiment for new reviews.

Code Example: Predicting Sentiment

# New reviews for prediction
reviews = [
    "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 reviews
inputs = tokenizer(reviews, truncation=True, padding=True, return_tensors="pt")

# Get predictions
outputs = model(**inputs)
predictions = outputs.logits.argmax(dim=-1)

# Map predictions to labels
labels = ["Negative", "Positive"]
for review, prediction in zip(reviews, predictions):
    print(f"Review: {review}")
    print(f"Predicted Sentiment: {labels[prediction]}")

9. Step 6: Using the Model for Prediction

Finally, we’ll use the trained model to predict sentiment for new reviews.

Code Example: Predicting Sentiment

# New reviews for prediction
reviews = [
    "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 reviews
inputs = tokenizer(reviews, truncation=True, padding=True, return_tensors="pt")

# Get predictions
outputs = model(**inputs)
predictions = outputs.logits.argmax(dim=-1)

# Map predictions to labels
labels = ["Negative", "Positive"]
for review, prediction in zip(reviews, predictions):
    print(f"Review: {review}")
    print(f"Predicted Sentiment: {labels[prediction]}")