View source on GitHub |
A preprocessing layer which buckets continuous features by ranges.
Inherits From: Layer
, Operation
tf.keras.layers.Discretization(
bin_boundaries=None,
num_bins=None,
epsilon=0.01,
output_mode='int',
sparse=False,
dtype=None,
name=None
)
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.
Input shape | |
---|---|
Any array of dimension 2 or higher. |
Output shape | |
---|---|
Same as input shape. |
Examples:
Discretize 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 = Discretization(bin_boundaries=[0., 1., 2.])
>>> layer(input)
array([[0, 2, 3, 1],
[1, 3, 2, 1]])
Discretize 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 = Discretization(num_bins=4, epsilon=0.01)
>>> layer.adapt(input)
>>> layer(input)
array([[0, 2, 3, 2],
[1, 3, 3, 1]])
Methods
adapt
adapt(
data, 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.
Arguments | |
---|---|
data
|
The data to train on. It can be passed either as a
batched tf.data.Dataset ,
or as a NumPy array.
|
steps
|
Integer or None .
Total number of steps (batches of samples) to process.
If data is a tf.data.Dataset , and steps is None ,
adapt() 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 or list inputs.
|
finalize_state
finalize_state()
from_config
@classmethod
from_config( config )
Creates a layer from its config.
This method is the reverse of get_config
,
capable of instantiating the same layer from the config
dictionary. It does not handle layer connectivity
(handled by Network), nor weights (handled by set_weights
).
Args | |
---|---|
config
|
A Python dictionary, typically the output of get_config. |
Returns | |
---|---|
A layer instance. |
reset_state
reset_state()
symbolic_call
symbolic_call(
*args, **kwargs
)
update_state
update_state(
data
)