IniziaInizia gratis

Evaluating the BERT model

Having tokenized the sample reviews using BERT's tokenizer, it's now time to evaluate the BERT model with the samples at PyBooks. Additionally, you will evaluate the model's sentiment prediction on new data.

The following has been imported for you: BertTokenizer, BertForSequenceClassification, torch. The trained model instance is also preloaded. We will now test it on a new data sample.

Questo esercizio fa parte del corso

Deep Learning for Text with PyTorch

Visualizza il corso

Istruzioni dell'esercizio

  • Prepare the evaluation text for the model by tokenizing it and returning PyTorch tensors.
  • Convert the output logits to probabilities between zero and one.
  • Display the sentiments from the probabilities.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

text = "I had an awesome day!"

# Tokenize the text and return PyTorch tensors
input_eval = tokenizer(____, return_tensors=____, truncation=True, padding=True, max_length=32)
outputs_eval = model(**input_eval)

# Convert the output logits to probabilities
predictions = torch.nn.functional.____(outputs_eval.____, dim=-1)

# Display the sentiments
predicted_label = ____ if torch.____(predictions) > 0 else ____
print(f"Text: {text}\nSentiment: {predicted_label}")
Modifica ed esegui il codice