{"record":{"id":"70b8e72a17b8fb88","repo":"rohitg00/ai-engineering-from-scratch","slug":"confidences-and-correct-must-have-the-same-shape","errorCode":null,"errorMessage":"confidences and correct must have the same shape","messagePattern":"confidences and correct must have the same shape","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/73-perplexity-calibration/code/main.py","lineNumber":59,"sourceCode":"            if nll < 0:\n                raise ValueError(\"neg_log_probs must be non-negative (did you forget the negation?)\")\n            if n < 0:\n                raise ValueError(\"token_counts must be non-negative\")\n            total_nll += float(nll)\n            total_tokens += int(n)\n        if total_tokens == 0:\n            return cls(perplexity=float(\"nan\"), avg_neg_log_likelihood=0.0, total_tokens=0)\n        avg_nll = total_nll / total_tokens\n        return cls(perplexity=math.exp(avg_nll), avg_neg_log_likelihood=avg_nll, total_tokens=total_tokens)\n\n\ndef perplexity(neg_log_probs: Sequence[float], token_counts: Sequence[int]) -> float:\n    return PerplexityResult.from_token_nll(neg_log_probs, token_counts).perplexity\n\n\ndef _validate_probs(confidences: np.ndarray, correct: np.ndarray) -> None:\n    if confidences.shape != correct.shape:\n        raise ValueError(\"confidences and correct must have the same shape\")\n    if confidences.ndim != 1:\n        raise ValueError(\"confidences must be 1-D\")\n    if confidences.size == 0:\n        return\n    if float(confidences.min()) < 0.0 or float(confidences.max()) > 1.0:\n        raise ValueError(\"confidences must lie in [0, 1]\")\n    uniq = set(np.unique(correct).tolist())\n    if not uniq.issubset({0, 1, 0.0, 1.0, True, False}):\n        raise ValueError(\"correct must be 0/1 or boolean\")\n\n\ndef _bin_indices(confidences: np.ndarray, n_bins: int) -> np.ndarray:\n    edges = np.linspace(0.0, 1.0, n_bins + 1)\n    idx = np.searchsorted(edges, confidences, side=\"right\") - 1\n    idx = np.clip(idx, 0, n_bins - 1)\n    return idx\n\n","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/73-perplexity-calibration/code/main.py#L41-L77","documentation":"Error \"confidences and correct must have the same shape\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/73-perplexity-calibration/code/main.py:59 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}