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

length mismatch: pred {len(predictions)} vs ref {len(referen

Error message

length mismatch: pred {len(predictions)} vs ref {len(references)}

What it means

Error "length mismatch: pred {len(predictions)} vs ref {len(references)}" thrown in rohitg00/ai-engineering-from-scratch.

Source

Thrown at phases/19-capstone-projects/63-multimodal-eval/code/main.py:102

    n = sim.shape[0]
    if k < 1 or k > n:
        raise ValueError(f"k {k} not in [1, N={n}]")

    targets = torch.arange(n, device=sim.device)

    topk_i2t = sim.topk(k, dim=1).indices
    hits_i2t = (topk_i2t == targets.unsqueeze(1)).any(dim=1).float().mean().item()

    sim_t = sim.T
    topk_t2i = sim_t.topk(k, dim=1).indices
    hits_t2i = (topk_t2i == targets.unsqueeze(1)).any(dim=1).float().mean().item()

    return hits_i2t, hits_t2i


def vqa_exact_match(predictions: list[int], references: list[int]) -> float:
    if len(predictions) != len(references):
        raise ValueError(f"length mismatch: pred {len(predictions)} vs ref {len(references)}")
    if not predictions:
        return 0.0
    hits = sum(1 for p, r in zip(predictions, references) if int(p) == int(r))
    return hits / len(predictions)


def _ngrams(seq: list[int], n: int) -> list[tuple[int, ...]]:
    if len(seq) < n:
        return []
    return [tuple(seq[i:i + n]) for i in range(len(seq) - n + 1)]


def _count(ngrams: list[tuple[int, ...]]) -> dict[tuple[int, ...], int]:
    out: dict[tuple[int, ...], int] = {}
    for g in ngrams:
        out[g] = out.get(g, 0) + 1
    return out

View on GitHub (pinned to 39ea8a1c6d)

When it happens

Trigger: Thrown at phases/19-capstone-projects/63-multimodal-eval/code/main.py:102 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/1a0ec87aa4dc23c7. Report an issue: GitHub.