rohitg00/ai-engineering-from-scratch · error · ValueError
k {k} not in [1, N={n}]
Error message
k {k} not in [1, N={n}] What it means
Error "k {k} not in [1, N={n}]" thrown in rohitg00/ai-engineering-from-scratch.
Source
Thrown at phases/19-capstone-projects/63-multimodal-eval/code/main.py:86
@dataclass
class EvalSuite:
retrieval: list[RetrievalPair]
vqa: list[VQATriple]
caps: list[CaptionSample]
def recall_at_k(sim: torch.Tensor, k: int) -> tuple[float, float]:
"""Return (i2t, t2i) recall@k.
sim is (N, N) where row i is the similarity of image i to every caption.
"""
if sim.dim() != 2 or sim.shape[0] != sim.shape[1]:
raise ValueError(f"sim must be square (N, N), got {tuple(sim.shape)}")
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.0View on GitHub (pinned to 39ea8a1c6d)
When it happens
Trigger: Thrown at phases/19-capstone-projects/63-multimodal-eval/code/main.py:86 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/69c9b65fa537a88e.
Report an issue: GitHub.