rohitg00/ai-engineering-from-scratch · error · ValueError
bleu4 requires at least one reference
Error message
bleu4 requires at least one reference
What it means
Error "bleu4 requires at least one reference" thrown in rohitg00/ai-engineering-from-scratch.
Source
Thrown at phases/19-capstone-projects/63-multimodal-eval/code/main.py:130
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
def bleu4(generated: list[int], references: list[list[int]],
smoothing: bool = True) -> float:
"""BLEU-4 against multiple reference captions.
Uses Chen and Cherry "method 1" smoothing when any n-gram precision is 0
and `smoothing` is True.
"""
if not references:
raise ValueError("bleu4 requires at least one reference")
if not generated:
return 0.0
weights = [0.25, 0.25, 0.25, 0.25]
log_p_sum = 0.0
for n in range(1, 5):
gen_ngrams = _ngrams(generated, n)
gen_counts = _count(gen_ngrams)
ref_max_counts: dict[tuple[int, ...], int] = {}
for ref in references:
ref_counts = _count(_ngrams(ref, n))
for g, c in ref_counts.items():
if c > ref_max_counts.get(g, 0):
ref_max_counts[g] = c
clipped = 0
for g, c in gen_counts.items():
clipped += min(c, ref_max_counts.get(g, 0))View on GitHub (pinned to 39ea8a1c6d)
When it happens
Trigger: Thrown at phases/19-capstone-projects/63-multimodal-eval/code/main.py:130 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/a73a443cce15fcec.
Report an issue: GitHub.