{"record":{"id":"437102ea6c7e35ea","repo":"keras-team/keras","slug":"arguments-target-and-output-must-have-the-same-437102","errorCode":null,"errorMessage":"Arguments `target` and `output` must have the same shape up until the last dimension: target.shape={target.shape}, output.shape={output.shape}","messagePattern":"Arguments `target` and `output` must have the same shape up until the last dimension: target\\.shape=(.+?), output\\.shape=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/numpy/nn.py","lineNumber":899,"sourceCode":"        output = np.clip(output, backend.epsilon(), 1.0 - backend.epsilon())\n        log_prob = np.log(output)\n    return -np.sum(target * log_prob, axis=axis)\n\n\ndef sparse_categorical_crossentropy(target, output, from_logits=False, axis=-1):\n    target = np.array(target, dtype=\"int32\")\n    output = np.array(output)\n    if len(target.shape) == len(output.shape) and target.shape[-1] == 1:\n        target = np.squeeze(target, axis=-1)\n\n    if len(output.shape) < 1:\n        raise ValueError(\n            \"Argument `output` must be at least rank 1. \"\n            \"Received: \"\n            f\"output.shape={output.shape}\"\n        )\n    if target.shape != output.shape[:-1]:\n        raise ValueError(\n            \"Arguments `target` and `output` must have the same shape \"\n            \"up until the last dimension: \"\n            f\"target.shape={target.shape}, output.shape={output.shape}\"\n        )\n    if from_logits:\n        log_prob = log_softmax(output, axis=axis)\n    else:\n        output = output / np.sum(output, axis, keepdims=True)\n        output = np.clip(output, backend.epsilon(), 1.0 - backend.epsilon())\n        log_prob = np.log(output)\n    target = one_hot(target, output.shape[axis], axis=axis)\n    return -np.sum(target * log_prob, axis=axis)\n\n\ndef binary_crossentropy(target, output, from_logits=False):\n    target = np.array(target)\n    output = np.array(output)\n","sourceCodeStart":881,"sourceCodeEnd":917,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/numpy/nn.py#L881-L917","documentation":"Raised by the NumPy backend's categorical crossentropy implementation when the shape guard that compares `target` and `output` finds they disagree on any dimension except the last one. The last axis of `output` holds class probabilities while `target` may be one-hot along that same axis, so all leading (batch/intermediate) dimensions must line up exactly; a mismatch means the labels tensor does not broadcast against the predictions tensor.","triggerScenarios":"Thrown at keras/src/backend/numpy/nn.py:899 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Reshape or re-encode `target` so every dimension except the last matches `output` (e.g. one-hot encode integer labels with keras.ops.one_hot to the same class count)","Remove a spurious trailing axis of size 1 on `target` (squeeze it) when labels were stored as (..., 1) instead of (..., num_classes)","Check for accidental broadcasting upstream in the model that changed the batch or intermediate dimensions of the predictions before the loss was computed"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}