rohitg00/ai-engineering-from-scratch · error · ValueError

predictions and references must align

Error message

predictions and references must align

What it means

Error "predictions and references must align" thrown in rohitg00/ai-engineering-from-scratch.

Source

Thrown at phases/19-capstone-projects/71-classical-metrics/code/main.py:170

        return accuracy(prediction, targets)
    if metric_name == "f1":
        return max(f1_score(prediction, t) for t in targets)
    if metric_name == "bleu_4":
        return max(bleu4(prediction, t) for t in targets)
    if metric_name == "rouge_l":
        return max(rouge_l(prediction, t) for t in targets)
    raise ValueError(f"unknown metric_name: {metric_name}")


def corpus_mean(scores: list[float]) -> float:
    if not scores:
        return 0.0
    return float(np.mean(scores))


def corpus_bleu(predictions: list[str], references: list[str], max_n: int = 4) -> float:
    if len(predictions) != len(references):
        raise ValueError("predictions and references must align")
    if not predictions:
        return 0.0
    total_cand_len = 0
    total_ref_len = 0
    clipped_sums = [0] * max_n
    total_sums = [0] * max_n
    for cand_text, ref_text in zip(predictions, references):
        cand = tokenize(cand_text)
        ref = tokenize(ref_text)
        total_cand_len += len(cand)
        total_ref_len += len(ref)
        for n in range(1, max_n + 1):
            clipped, total = _modified_precision(cand, ref, n)
            clipped_sums[n - 1] += clipped
            total_sums[n - 1] += total
    if total_cand_len == 0:
        return 0.0
    log_p_sum = 0.0

View on GitHub (pinned to 39ea8a1c6d)

When it happens

Trigger: Thrown at phases/19-capstone-projects/71-classical-metrics/code/main.py:170 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/8b8e3317104abadb. Report an issue: GitHub.