Views
No views yet
| Training Loss | Epoch | Step | Validation Loss |
|---|---|---|---|
| 2.4912 | 1.0 | 625 | 2.3564 |
| 2.4209 | 2.0 | 1250 | 2.3311 |
| 2.4 | 3.0 | 1875 | 2.3038 |
1import torch
2import pandas as pd
3from transformers import AutoTokenizer, AutoModelForMaskedLM
4
5# Load the tokenizer and model
6tokenizer = AutoTokenizer.from_pretrained("Francesco-A/distilbert-base-uncased-finetuned-imdb-v2")
7model = AutoModelForMaskedLM.from_pretrained("Francesco-A/distilbert-base-uncased-finetuned-imdb-v2")
8
9# Example sentence
10sentence = "This movie is really [MASK]."
11
12# Tokenize the sentence
13inputs = tokenizer(sentence, return_tensors="pt")
14
15# Get the model's predictions
16with torch.no_grad():
17 outputs = model(**inputs)
18
19# Get the top-k predicted tokens and their probabilities
20k = 5 # Number of top predictions to retrieve
21masked_token_index = inputs["input_ids"].tolist()[0].index(tokenizer.mask_token_id)
22predicted_token_logits = outputs.logits[0, masked_token_index]
23topk_values, topk_indices = torch.topk(torch.softmax(predicted_token_logits, dim=-1), k)
24
25# Convert top predicted token indices to words
26predicted_tokens = [tokenizer.decode(idx.item()) for idx in topk_indices]
27# Convert probabilities to Python floats
28probs = topk_values.tolist()
29
30# Create a DataFrame to display the top predicted words and probabilities
31data = {
32 "Predicted Words": predicted_tokens,
33 "Probability": probs,
34}
35
36df = pd.DataFrame(data)
37
38# Display the DataFrame
39df
40