Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output int64 by default from Discretization #14841

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions keras/layers/preprocessing/discretization.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,18 @@ class Discretization(base_preprocessing_layer.PreprocessingLayer):
>>> 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=int32, numpy=
<tf.Tensor: shape=(2, 4), dtype=int64, numpy=
array([[0, 2, 3, 1],
[1, 3, 2, 1]], dtype=int32)>
[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=int32, numpy=
<tf.Tensor: shape=(2, 4), dtype=int64, numpy=
array([[0, 2, 3, 2],
[1, 3, 3, 1]], dtype=int32)>
[1, 3, 3, 1]], dtype=int64)>
"""

def __init__(self,
Expand Down Expand Up @@ -263,8 +263,11 @@ def compute_output_signature(self, input_spec):

def call(self, inputs):
def bucketize(inputs):
return tf.raw_ops.Bucketize(
outputs = tf.raw_ops.Bucketize(
input=inputs, boundaries=self.bin_boundaries)
# All other preprocessing layers use int64 for int output, so we conform
# here. Sadly the underlying op only supports int32, so we need to cast.
return tf.cast(outputs, tf.int64)

if tf_utils.is_ragged(inputs):
integer_buckets = tf.ragged.map_flat_values(bucketize, inputs)
Expand Down
6 changes: 6 additions & 0 deletions keras/layers/preprocessing/discretization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ def test_output_shape(self):
output = layer(input_data)
self.assertAllEqual(output.shape.as_list(), [16, 4])

def test_output_dtype(self):
input_data = keras.Input(batch_size=16, shape=(4,), dtype=tf.int64)
layer = discretization.Discretization(bin_boundaries=[-.5, 0.5, 1.5])
output = layer(input_data)
self.assertAllEqual(output.dtype, tf.int64)

def test_num_bins_negative_fails(self):
with self.assertRaisesRegex(ValueError, "`num_bins` must be.*num_bins=-7"):
_ = discretization.Discretization(num_bins=-7)
Expand Down