{"record":{"id":"5bd0a70c3c217a9b","repo":"TheAlgorithms/Python","slug":"sentence-length-of-y-true-and-y-pred-must-be-equal","errorCode":null,"errorMessage":"Sentence length of y_true and y_pred must be equal.","messagePattern":"Sentence length of y_true and y_pred must be equal\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"machine_learning/loss_functions.py","lineNumber":558,"sourceCode":"    >>> y_true = np.array([[1, 4]])\n    >>> y_pred = np.array(\n    ...    [[[0.28, 0.19, 0.21 , 0.15, 0.15],\n    ...      [0.24, 0.19, 0.09, 0.18, 0.27]],\n    ...      [[0.03, 0.26, 0.21, 0.18, 0.30],\n    ...       [0.28, 0.10, 0.33, 0.15, 0.12]]]\n    ... )\n    >>> perplexity_loss(y_true, y_pred)\n    Traceback (most recent call last):\n    ...\n    ValueError: Batch size of y_true and y_pred must be equal.\n    \"\"\"\n\n    vocab_size = y_pred.shape[2]\n\n    if y_true.shape[0] != y_pred.shape[0]:\n        raise ValueError(\"Batch size of y_true and y_pred must be equal.\")\n    if y_true.shape[1] != y_pred.shape[1]:\n        raise ValueError(\"Sentence length of y_true and y_pred must be equal.\")\n    if np.max(y_true) > vocab_size:\n        raise ValueError(\"Label value must not be greater than vocabulary size.\")\n\n    # Matrix to select prediction value only for true class\n    filter_matrix = np.array(\n        [[list(np.eye(vocab_size)[word]) for word in sentence] for sentence in y_true]\n    )\n\n    # Getting the matrix containing prediction for only true class\n    true_class_pred = np.sum(y_pred * filter_matrix, axis=2).clip(epsilon, 1)\n\n    # Calculating perplexity for each sentence\n    perp_losses = np.exp(np.negative(np.mean(np.log(true_class_pred), axis=1)))\n\n    return np.mean(perp_losses)\n\n\ndef smooth_l1_loss(y_true: np.ndarray, y_pred: np.ndarray, beta: float = 1.0) -> float:","sourceCodeStart":540,"sourceCodeEnd":576,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/loss_functions.py#L540-L576","documentation":"Thrown by perplexity_loss when the sentence-length dimension (shape[1]) of y_true and y_pred differ. Each row of y_true must have one token id per timestep that y_pred scores over the vocabulary, so the sequence lengths must match exactly.","triggerScenarios":"Calling perplexity_loss where y_true.shape[1] != y_pred.shape[1], e.g. 4 reference tokens against 5 prediction timesteps (often after truncation/padding applied to one side only).","commonSituations":"Padding reference sequences to a different max_length than the model input; truncation strategies (keep-first vs keep-last) applied inconsistently; BOS/EOS token added to one array but not the other.","solutions":["Pad or truncate y_true to the same length as y_pred's timestep axis (or regenerate both with one shared max_length).","Check the tokenizer/padding config: the same seq_len must be used for references and model outputs.","Trim to the shorter length if the extra steps are padding: y_pred = y_pred[:, :y_true.shape[1], :]."],"exampleFix":"# before\nloss = perplexity_loss(y_true, y_pred)  # y_true len 4, y_pred timesteps 5\n\n# after\ny_pred = y_pred[:, : y_true.shape[1], :]\nloss = perplexity_loss(y_true, y_pred)","handlingStrategy":"validation","validationCode":"if y_true.shape[1] != y_pred.shape[1]:\n    y_pred = y_pred[:, : y_true.shape[1], :]\nloss = perplexity_loss(y_true, y_pred)","typeGuard":"def perplexity_shapes_ok(y_true: np.ndarray, y_pred: np.ndarray) -> bool:\n    return (\n        y_pred.ndim == 3\n        and y_true.ndim == 2\n        and y_true.shape == y_pred.shape[:2]\n    )","tryCatchPattern":null,"preventionTips":["Use one shared max_length constant for padding references and model inputs.","Apply identical truncation strategy (head/tail) to both arrays.","Add or strip BOS/EOS tokens on both sides consistently."],"tags":["machine-learning","loss-function","nlp","padding"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}