|View source on GitHub|
Buckets data into discrete ranges.
See Migration guide for more details.
tf.keras.layers.Discretization( bin_boundaries=None, num_bins=None, epsilon=0.01, **kwargs )
Used in the notebooks
|Used in the guide||Used in the tutorials|
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.
Same as input shape.
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]], dtype=int64)>
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]], dtype=int64)>
A list of bin boundaries. The leftmost and rightmost bins
will always extend to
The integer number of bins to compute. If this option is set,
||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.|
||Whether the layer has been fit to data already.|
adapt( data, batch_size=None, steps=None )
Fits the state of the preprocessing layer to the data being passed.
adapt on a layer, a preprocessing layer's state will not
update during training. In order to make preprocessing layers efficient in
any distribution context, they are kept constant with respect to any
tf.Graphs that call the layer. This does not affect the layer use
when adapting each layer only once, but if you adapt a layer multiple times
you will need to take care to re-compile any compiled functions as follows:
- If you are adding a preprocessing layer to a
keras.Model, you need to call
model.compileafter each subsequent call to
- If you are calling a preprocessing layer inside
tf.data.Dataset.map, you should call
mapagain on the input
- If you are using a
tf.functiondirectly which calls a preprocessing layer, you need to call
tf.functionagain on your callable after each subsequent call to