When running the text-classification inference loop from the book, the output returned by the pipeline is a single dictionary instead of a list of dictionaries, causing a KeyError when trying to access output[0].
Code from the book
y_pred = []
for output in tqdm(pipe(KeyDataset(data["test"], "text")), total=len(data["test"])):
negative_score = output[0]["score"]
positive_score = output[2]["score"]
assignment = np.argmax([negative_score, positive_score])
y_pred.append(assignment)
Expected Output
[{"label": "negative", "score": 0.73}, {"label": "neutral", "score": 0.15}, {"label": "positive", "score": 0.12}]
Actual Output
{"label": "positive", "score": 0.95}
Error
My Fix
Replace return_all_scores=True with top_k=None:
pipe = pipeline(
model=model_path,
tokenizer=model_path,
top_k=None, # replaces deprecated return_all_scores=True
device="cuda:0"
)
When running the text-classification inference loop from the book, the
outputreturned by the pipeline is a single dictionary instead of a list of dictionaries, causing aKeyErrorwhen trying to accessoutput[0].Code from the book
Expected Output
[{"label": "negative", "score": 0.73}, {"label": "neutral", "score": 0.15}, {"label": "positive", "score": 0.12}]Actual Output
{"label": "positive", "score": 0.95}Error
My Fix
Replace
return_all_scores=Truewithtop_k=None: