{"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":"Raised by the deprecated sparse_categorical_crossentropy backend function when an explicit axis argument is used but the output tensor's rank is unknown at graph-construction time. Computing the axis permutation that moves the class axis last requires the rank, so with a symbolic shape of unknown rank (ndims is None) the function cannot proceed.","triggerScenarios":"Thrown at keras/src/legacy/backend.py:1969 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Ensure the model output tensor has a statically known rank (avoid ops that produce unknown-rank results, and build the model with fixed input shapes)","Pass axis=-1 when the class axis is already last, which avoids needing a permutation","Call the loss on concrete tensors (eager mode) where ranks are always known"],"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"}