Menu iconMenu icon
NLP with Transformers: Fundamentals and Core Applications

Project 1: Sentiment Analysis with BERT

9. Paso 6: Usando el Modelo para Predicción

Finalmente, usaremos el modelo entrenado para predecir el sentimiento de nuevas reseñas.

Ejemplo de Código: Prediciendo Sentimiento

# 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. Paso 6: Usando el Modelo para Predicción

Finalmente, usaremos el modelo entrenado para predecir el sentimiento de nuevas reseñas.

Ejemplo de Código: Prediciendo Sentimiento

# 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. Paso 6: Usando el Modelo para Predicción

Finalmente, usaremos el modelo entrenado para predecir el sentimiento de nuevas reseñas.

Ejemplo de Código: Prediciendo Sentimiento

# 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. Paso 6: Usando el Modelo para Predicción

Finalmente, usaremos el modelo entrenado para predecir el sentimiento de nuevas reseñas.

Ejemplo de Código: Prediciendo Sentimiento

# 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]}")