rohitg00/ai-engineering-from-scratch · error · ValueError
token_counts must be non-negative
Error message
token_counts must be non-negative
What it means
Error "token_counts must be non-negative" thrown in rohitg00/ai-engineering-from-scratch.
Source
Thrown at phases/19-capstone-projects/73-perplexity-calibration/code/main.py:44
def to_dict(self) -> dict:
return {
"perplexity": self.perplexity,
"avg_neg_log_likelihood": self.avg_neg_log_likelihood,
"total_tokens": self.total_tokens,
}
@classmethod
def from_token_nll(cls, neg_log_probs: Sequence[float], token_counts: Sequence[int]) -> "PerplexityResult":
if len(neg_log_probs) != len(token_counts):
raise ValueError("neg_log_probs and token_counts must align")
total_nll = 0.0
total_tokens = 0
for nll, n in zip(neg_log_probs, token_counts):
if nll < 0:
raise ValueError("neg_log_probs must be non-negative (did you forget the negation?)")
if n < 0:
raise ValueError("token_counts must be non-negative")
total_nll += float(nll)
total_tokens += int(n)
if total_tokens == 0:
return cls(perplexity=float("nan"), avg_neg_log_likelihood=0.0, total_tokens=0)
avg_nll = total_nll / total_tokens
return cls(perplexity=math.exp(avg_nll), avg_neg_log_likelihood=avg_nll, total_tokens=total_tokens)
def perplexity(neg_log_probs: Sequence[float], token_counts: Sequence[int]) -> float:
return PerplexityResult.from_token_nll(neg_log_probs, token_counts).perplexity
def _validate_probs(confidences: np.ndarray, correct: np.ndarray) -> None:
if confidences.shape != correct.shape:
raise ValueError("confidences and correct must have the same shape")
if confidences.ndim != 1:
raise ValueError("confidences must be 1-D")
if confidences.size == 0:View on GitHub (pinned to 39ea8a1c6d)
When it happens
Trigger: Thrown at phases/19-capstone-projects/73-perplexity-calibration/code/main.py:44 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/79955cffde535808.
Report an issue: GitHub.