{"record":{"id":"e93cb3c58742d800","repo":"keras-team/keras","slug":"cannot-apply-softmax-to-a-tensor-that-is-1d-recei","errorCode":null,"errorMessage":"Cannot apply softmax to a tensor that is 1D. Received input: {x}","messagePattern":"Cannot apply softmax to a tensor that is 1D\\. Received input: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/backend.py","lineNumber":1910,"sourceCode":"\n\n@keras_export(\"keras._legacy.backend.sign\")\ndef sign(x):\n    \"\"\"DEPRECATED.\"\"\"\n    return tf.sign(x)\n\n\n@keras_export(\"keras._legacy.backend.sin\")\ndef sin(x):\n    \"\"\"DEPRECATED.\"\"\"\n    return tf.sin(x)\n\n\n@keras_export(\"keras._legacy.backend.softmax\")\ndef softmax(x, axis=-1):\n    \"\"\"DEPRECATED.\"\"\"\n    if x.shape.rank <= 1:\n        raise ValueError(\n            f\"Cannot apply softmax to a tensor that is 1D. Received input: {x}\"\n        )\n\n    if isinstance(axis, int):\n        output = tf.nn.softmax(x, axis=axis)\n    else:\n        # nn.softmax does not support tuple axis.\n        numerator = tf.exp(x - tf.reduce_max(x, axis=axis, keepdims=True))\n        denominator = tf.reduce_sum(numerator, axis=axis, keepdims=True)\n        output = numerator / denominator\n\n    # Cache the logits to use for crossentropy loss.\n    output._keras_logits = x\n    return output\n\n\n@keras_export(\"keras._legacy.backend.softplus\")\ndef softplus(x):","sourceCodeStart":1892,"sourceCodeEnd":1928,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/backend.py#L1892-L1928","documentation":"Raised by the deprecated backend.softmax when the input tensor's rank is <= 1, i.e. it is a flat vector with no batch/feature axis distinction. Softmax over a 1D tensor is considered ill-defined in this API (it would softmax over the only axis, which is usually a user mistake), so the guard rejects it before calling tf.nn.softmax.","triggerScenarios":"Thrown at keras/src/legacy/backend.py:1910 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Reshape the 1D vector to 2D before calling softmax, e.g. x = tf.expand_dims(x, axis=0), apply softmax, then squeeze the added axis back","Double-check upstream data: a rank-1 tensor here often means a batch dimension was lost in an earlier reshape/reduction"],"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"}