{"record":{"id":"127b0493c7384123","repo":"keras-team/keras","slug":"sparse-true-can-only-be-used-with-the-tensorflow","errorCode":null,"errorMessage":"`sparse=True` can only be used with the TensorFlow backend.","messagePattern":"`sparse=True` can only be used with the TensorFlow backend\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/hashed_crossing.py","lineNumber":92,"sourceCode":"        num_bins,\n        output_mode=\"int\",\n        sparse=False,\n        name=None,\n        dtype=None,\n        **kwargs,\n    ):\n        if not tf.available:\n            raise ImportError(\n                \"Layer HashedCrossing requires TensorFlow. \"\n                \"Install it via `pip install tensorflow`.\"\n            )\n\n        if output_mode == \"int\" and dtype is None:\n            dtype = \"int64\"\n\n        super().__init__(name=name, dtype=dtype)\n        if sparse and backend.backend() != \"tensorflow\":\n            raise ValueError(\n                \"`sparse=True` can only be used with the TensorFlow backend.\"\n            )\n\n        argument_validation.validate_string_arg(\n            output_mode,\n            allowable_strings=(\"int\", \"one_hot\"),\n            caller_name=self.__class__.__name__,\n            arg_name=\"output_mode\",\n        )\n\n        self.num_bins = num_bins\n        self.output_mode = output_mode\n        self.sparse = sparse\n        self._allow_non_tensor_positional_args = True\n        self._convert_input_args = False\n        self.supports_jit = False\n\n    def compute_output_shape(self, input_shape):","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/hashed_crossing.py#L74-L110","documentation":"Sparse output for HashedCrossing is implemented only for the TensorFlow backend (it returns tf.SparseTensors). Constructing the layer with sparse=True under jax or torch is rejected.","triggerScenarios":"layers.HashedCrossing(num_bins=..., sparse=True) while keras.config.backend() is 'jax' or 'torch'.","commonSituations":"Setting KERAS_BACKEND=jax or torch and constructing layers.Hashing/HashedCrossing with sparse=True to save memory on wide one-hot features.","solutions":["Set sparse=False and accept dense output","Switch to the tensorflow backend (KERAS_BACKEND=tensorflow) if sparse tensors are required","Post-process dense output into your framework's sparse representation downstream"],"exampleFix":"// before\nKERAS_BACKEND=jax ... layer = HashedCrossing(num_bins=1000, sparse=True)\n// after\nlayer = HashedCrossing(num_bins=1000, sparse=False)\n# or run with KERAS_BACKEND=tensorflow","handlingStrategy":"validation","validationCode":"from keras.src import backend\nassert backend.backend() == \"tensorflow\" or not sparse, \"sparse=True requires TF backend\"","typeGuard":"def sparse_allowed():\n    from keras.src import backend\n    return backend.backend() == \"tensorflow\"","tryCatchPattern":"catch ValueError and rerun with sparse=False, converting the dense output to sparse downstream if needed","preventionTips":["Only set sparse=True when backend.backend() == 'tensorflow'","Pre-compute dense outputs or store indices separately on other backends"],"tags":["keras","hashed-crossing","sparse","backend"],"backgroundTag":"backend-not-supported","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}