Skip to content

Chapter 04 - text-classification pipeline returns single dict instead of list #107

@HLZHarry

Description

@HLZHarry

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

KeyError: 0

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"
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions