# tf.keras.layers.Discretization

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v2.13.1/keras/layers/preprocessing/discretization.py#L136-L444) |

A preprocessing layer which buckets continuous features by ranges.

Inherits From: [`PreprocessingLayer`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/keras/layers/experimental/preprocessing/PreprocessingLayer), [`Layer`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/keras/layers/Layer), [`Module`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/Module)

#### View aliases


**Main aliases**

[`tf.keras.layers.experimental.preprocessing.Discretization`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Discretization)
**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

\`tf.compat.v1.keras.layers.Discretization\`, \`tf.compat.v1.keras.layers.experimental.preprocessing.Discretization\`

<br />

    tf.keras.layers.Discretization(
        bin_boundaries=None,
        num_bins=None,
        epsilon=0.01,
        output_mode='int',
        sparse=False,
        **kwargs
    )

This layer will place each element of its input data into one of several
contiguous ranges and output an integer index indicating which range each
element was placed in.

For an overview and full list of preprocessing layers, see the preprocessing
[guide](https://www.tensorflow.org/guide/keras/preprocessing_layers).

<br />

<br />

<br />

| ## Input shape ||
|---|---|
| Any [`tf.Tensor`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/Tensor) or [`tf.RaggedTensor`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/RaggedTensor) of dimension 2 or higher. ||

<br />

<br />

<br />

<br />

| ## Output shape ||
|---|---|
| Same as input shape. ||

<br />

<br />

<br />

<br />

| ## Arguments ||
|---|---|
| `bin_boundaries` | A list of bin boundaries. The leftmost and rightmost bins will always extend to `-inf` and `inf`, so `bin_boundaries=[0., 1., 2.]` generates bins `(-inf, 0.)`, `[0., 1.)`, `[1., 2.)`, and `[2., +inf)`. If this option is set, `adapt()` should not be called. |
| `num_bins` | The integer number of bins to compute. If this option is set, `adapt()` should be called to learn the bin boundaries. |
| `epsilon` | Error tolerance, typically a small fraction close to zero (e.g. 0.01). Higher values of epsilon increase the quantile approximation, and hence result in more unequal buckets, but could improve performance and resource consumption. |
| `output_mode` | Specification for the output of the layer. Values can be `"int"`, `"one_hot"`, `"multi_hot"`, or `"count"` configuring the layer as follows: <br /> - `"int"`: Return the discretized bin indices directly. - `"one_hot"`: Encodes each individual element in the input into an array the same size as `num_bins`, containing a 1 at the input's bin index. If the last dimension is size 1, will encode on that dimension. If the last dimension is not size 1, will append a new dimension for the encoded output. - `"multi_hot"`: Encodes each sample in the input into a single array the same size as `num_bins`, containing a 1 for each bin index index present in the sample. Treats the last dimension as the sample dimension, if input shape is `(..., sample_length)`, output shape will be `(..., num_tokens)`. - `"count"`: As `"multi_hot"`, but the int array contains a count of the number of times the bin index appeared in the sample. Defaults to `"int"`. |
| `sparse` | Boolean. Only applicable to `"one_hot"`, `"multi_hot"`, and `"count"` output modes. If True, returns a `SparseTensor` instead of a dense `Tensor`. Defaults to `False`. |

#### Examples:

Bucketize float values based on provided buckets.

    >>> input = np.array([[-1.5, 1.0, 3.4, .5], [0.0, 3.0, 1.3, 0.0]])
    >>> layer = tf.keras.layers.Discretization(bin_boundaries=[0., 1., 2.])
    >>> layer(input)
    <tf.Tensor: shape=(2, 4), dtype=int64, numpy=
    array([[0, 2, 3, 1],
           [1, 3, 2, 1]])>

Bucketize float values based on a number of buckets to compute.

    >>> input = np.array([[-1.5, 1.0, 3.4, .5], [0.0, 3.0, 1.3, 0.0]])
    >>> layer = tf.keras.layers.Discretization(num_bins=4, epsilon=0.01)
    >>> layer.adapt(input)
    >>> layer(input)
    <tf.Tensor: shape=(2, 4), dtype=int64, numpy=
    array([[0, 2, 3, 2],
           [1, 3, 3, 1]])>

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `is_adapted` | Whether the layer has been fit to data already. |

<br />

## Methods

### `adapt`

[View source](https://github.com/keras-team/keras/tree/v2.13.1/keras/layers/preprocessing/discretization.py#L315-L361)

    adapt(
        data, batch_size=None, steps=None
    )

Computes bin boundaries from quantiles in a input dataset.

Calling `adapt()` on a `Discretization` layer is an alternative to
passing in a `bin_boundaries` argument during construction. A
`Discretization` layer should always be either adapted over a dataset or
passed `bin_boundaries`.

During `adapt()`, the layer will estimate the quantile boundaries of the
input dataset. The number of quantiles can be controlled via the
`num_bins` argument, and the error tolerance for quantile boundaries can
be controlled via the `epsilon` argument.

In order to make `Discretization` efficient in any distribution context,
the computed boundaries are kept static with respect to any compiled
[`tf.Graph`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/Graph)s that call the layer. As a consequence, if the layer is
adapted a second time, any models using the layer should be re-compiled.
For more information see
[`tf.keras.layers.experimental.preprocessing.PreprocessingLayer.adapt`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/keras/layers/experimental/preprocessing/PreprocessingLayer#adapt).

`adapt()` is meant only as a single machine utility to compute layer
state. To analyze a dataset that cannot fit on a single machine, see
[Tensorflow Transform](https://www.tensorflow.org/tfx/transform/get_started) for a
multi-machine, map-reduce solution.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `data` | The data to train on. It can be passed either as a [`tf.data.Dataset`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/data/Dataset), or as a numpy array. |
| `batch_size` | Integer or `None`. Number of samples per state update. If unspecified, `batch_size` will default to 32. Do not specify the `batch_size` if your data is in the form of datasets, generators, or [`keras.utils.Sequence`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/keras/utils/Sequence) instances (since they generate batches). |
| `steps` | Integer or `None`. Total number of steps (batches of samples) When training with input tensors such as TensorFlow data tensors, the default `None` is equal to the number of samples in your dataset divided by the batch size, or 1 if that cannot be determined. If x is a [`tf.data`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/data) dataset, and 'steps' is None, the epoch will run until the input dataset is exhausted. When passing an infinitely repeating dataset, you must specify the `steps` argument. This argument is not supported with array inputs. |

<br />

### `compile`

[View source](https://github.com/keras-team/keras/tree/v2.13.1/keras/engine/base_preprocessing_layer.py#L139-L160)

    compile(
        run_eagerly=None, steps_per_execution=None
    )

Configures the layer for `adapt`.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `run_eagerly` | Bool. Defaults to `False`. If `True`, this `Model`'s logic will not be wrapped in a [`tf.function`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/function). Recommended to leave this as `None` unless your `Model` cannot be run inside a [`tf.function`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/function). |
| `steps_per_execution` | Int. Defaults to 1. The number of batches to run during each [`tf.function`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/function) call. Running multiple batches inside a single [`tf.function`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/function) call can greatly improve performance on TPUs or small models with a large Python overhead. |

<br />

### `reset_state`

[View source](https://github.com/keras-team/keras/tree/v2.13.1/keras/layers/preprocessing/discretization.py#L391-L395)

    reset_state()

Resets the statistics of the preprocessing layer.

### `update_state`

[View source](https://github.com/keras-team/keras/tree/v2.13.1/keras/layers/preprocessing/discretization.py#L363-L380)

    update_state(
        data
    )

Accumulates statistics for the preprocessing layer.

<br />

<br />

<br />

| Arguments ||
|---|---|
| `data` | A mini-batch of inputs to the layer. |

<br />