{"record":{"id":"7a39fad65fea4015","repo":"keras-team/keras","slug":"cannot-compute-sparse-categorical-crossentropy-wit","errorCode":null,"errorMessage":"Cannot compute sparse categorical crossentropy with `axis={}` on an output tensor with unknown rank","messagePattern":"Cannot compute sparse categorical crossentropy with `axis=(.+?)` on an output tensor with unknown rank","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/backend.py","lineNumber":1969,"sourceCode":"        output = tf.clip_by_value(output, epsilon_, 1 - epsilon_)\n        output = tf.math.log(output)\n\n    # Permute output so that the last axis contains the logits/probabilities.\n    if isinstance(output.shape, (tuple, list)):\n        output_rank = len(output.shape)\n    else:\n        output_rank = output.shape.ndims\n    if output_rank is not None:\n        axis %= output_rank\n        if axis != output_rank - 1:\n            permutation = list(\n                itertools.chain(\n                    range(axis), range(axis + 1, output_rank), [axis]\n                )\n            )\n            output = tf.transpose(output, perm=permutation)\n    elif axis != -1:\n        raise ValueError(\n            \"Cannot compute sparse categorical crossentropy with `axis={}` \"\n            \"on an output tensor with unknown rank\".format(axis)\n        )\n\n    # Try to adjust the shape so that rank of labels = rank of logits - 1.\n    output_shape = tf.shape(output)\n    target_rank = target.shape.ndims\n\n    update_shape = (\n        target_rank is not None\n        and output_rank is not None\n        and target_rank != output_rank - 1\n    )\n    if update_shape:\n        target = flatten(target)\n        output = tf.reshape(output, [-1, output_shape[-1]])\n\n    if ignore_class is not None:","sourceCodeStart":1951,"sourceCodeEnd":1987,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/backend.py#L1951-L1987","documentation":"Error \"Cannot compute sparse categorical crossentropy with `axis={}` on an output tensor with unknown rank\" thrown in keras-team/keras.","triggerScenarios":"Thrown at keras/src/legacy/backend.py:1969 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":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}