{"record":{"id":"3a787e4216387ccc","repo":"keras-team/keras","slug":"argument-num-segments-cannot-be-set-when-sorted","errorCode":null,"errorMessage":"Argument `num_segments` cannot be set when sorted is True when using the tensorflow backend.Received: num_segments={num_segments}, sorted={sorted}.","messagePattern":"Argument `num_segments` cannot be set when sorted is True when using the tensorflow backend\\.Received: num_segments=(.+?), sorted=(.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/tensorflow/math.py","lineNumber":12,"sourceCode":"import tensorflow as tf\n\nfrom keras.src.backend import standardize_dtype\nfrom keras.src.backend.common import dtypes\nfrom keras.src.backend.tensorflow.core import cast\nfrom keras.src.backend.tensorflow.core import convert_to_tensor\n\n\ndef segment_sum(data, segment_ids, num_segments=None, sorted=False):\n    if sorted:\n        if num_segments is not None:\n            raise ValueError(\n                \"Argument `num_segments` cannot be set when sorted is True \"\n                \"when using the tensorflow backend.\"\n                f\"Received: num_segments={num_segments}, sorted={sorted}.\"\n            )\n        return tf.math.segment_sum(data, segment_ids)\n    else:\n        if num_segments is None:\n            unique_segment_ids, _ = tf.unique(segment_ids)\n            num_segments = tf.shape(unique_segment_ids)[0]\n        return tf.math.unsorted_segment_sum(data, segment_ids, num_segments)\n\n\ndef segment_max(data, segment_ids, num_segments=None, sorted=False):\n    if sorted:\n        if num_segments is not None:\n            raise ValueError(\n                \"Argument `num_segments` cannot be set when sorted is True \"\n                \"when using the tensorflow backend.\"","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/tensorflow/math.py#L1-L30","documentation":"Raised at the top of keras/src/backend/tensorflow/math.py segment_sum: this is a backend-specific restriction — TensorFlow's unsorted segment ops cannot combine a pre-specified number of output segments with a sorted input in one call. The guard fires when both num_segments is not None and sorted=True, before any reduction is performed.","triggerScenarios":"Thrown at keras/src/backend/tensorflow/math.py:12 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Omit num_segments when using sorted=True","Or use sorted=False (default) and pass num_segments explicitly","Compute the segment count yourself (e.g. num_ids = tf.reduce_max(segment_ids)+1) if you need it with sorted data"],"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"}