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

Add digitize op for Discretization layer #641

Merged
merged 4 commits into from
Aug 1, 2023
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
6 changes: 6 additions & 0 deletions keras_core/backend/jax/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ def diagonal(x, offset=0, axis1=0, axis2=1):
)


def digitize(x, bins):
x = convert_to_tensor(x)
bins = convert_to_tensor(bins)
return cast(jnp.digitize(x, bins), "int64")


def dot(x, y):
return jnp.dot(x, y)

Expand Down
4 changes: 4 additions & 0 deletions keras_core/backend/numpy/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ def diagonal(x, offset=0, axis1=0, axis2=1):
)


def digitize(x, bins):
return np.digitize(x, bins)


def dot(x, y):
return np.dot(x, y)

Expand Down
6 changes: 6 additions & 0 deletions keras_core/backend/tensorflow/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ def diagonal(x, offset=0, axis1=0, axis2=1):
)


def digitize(x, bins):
x = convert_to_tensor(x)
bins = list(bins)
return tf.cast(tf.raw_ops.Bucketize(input=x, boundaries=bins), "int64")


def dot(x, y):
return tfnp.dot(x, y)

Expand Down
6 changes: 6 additions & 0 deletions keras_core/backend/torch/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ def diagonal(x, offset=0, axis1=0, axis2=1):
)


def digitize(x, bins):
x = convert_to_tensor(x)
bins = convert_to_tensor(bins)
return cast(torch.bucketize(x, bins, right=True), "int32")


def dot(x, y):
x, y = convert_to_tensor(x), convert_to_tensor(y)
if x.ndim == 0 or y.ndim == 0:
Expand Down
38 changes: 38 additions & 0 deletions keras_core/ops/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
diag
diagonal
diff
digitize
divide
dot
dtype
Expand Down Expand Up @@ -1534,6 +1535,43 @@ def diagonal(x, offset=0, axis1=0, axis2=1):
)


class Digitize(Operation):
def call(self, x, bins):
return backend.numpy.digitize(x, bins)

def compute_output_spec(self, x, bins):
bins_shape = bins.shape
if len(bins_shape) > 1:
raise ValueError(
"`bins` must be an array of one dimension, but recieved `bins` "
f"of shape {bins_shape}."
)
return KerasTensor(x.shape, dtype="int")


@keras_core_export(["keras_core.ops.digitize", "keras_core.ops.numpy.digitize"])
def digitize(x, bins):
"""Returns the indices of the bins to which each value in `x` belongs.

Args:
x: Input array to be binned.
bins: Array of bins. It has to be one-dimensional and monotonically
increasing.

Returns:
Output array of indices, of same shape as x.

Examples:
>>> x = np.array([0.0, 1.0, 3.0, 1.6])
>>> bins = np.array([0.0, 3.0, 4.5, 7.0])
>>> keras_core.ops.digitize(x, bins)
array([1, 1, 2, 1])
"""
if any_symbolic_tensors((x, bins)):
return Digitize().symbolic_call(x, bins)
return backend.numpy.digitize(x, bins)


class Dot(Operation):
def call(self, x1, x2):
return backend.numpy.dot(x1, x2)
Expand Down
52 changes: 52 additions & 0 deletions keras_core/ops/numpy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from keras_core import backend
from keras_core import testing
from keras_core.backend.common import standardize_dtype
from keras_core.backend.common.keras_tensor import KerasTensor
from keras_core.ops import numpy as knp

Expand Down Expand Up @@ -660,6 +661,17 @@ def test_xor(self):
y = KerasTensor([2, 3, 4])
knp.logical_xor(x, y)

def test_digitize(self):
x = KerasTensor((2, 3))
bins = KerasTensor((3,))
self.assertEqual(knp.digitize(x, bins).shape, (2, 3))
abuelnasr0 marked this conversation as resolved.
Show resolved Hide resolved
self.assertTrue(knp.digitize(x, bins).dtype == standardize_dtype("int"))

with self.assertRaises(ValueError):
x = KerasTensor([2, 3])
bins = KerasTensor([2, 3, 4])
knp.digitize(x, bins)


class NumpyOneInputOpsDynamicShapeTest(testing.TestCase):
def test_mean(self):
Expand Down Expand Up @@ -2092,6 +2104,46 @@ def test_where(self):
self.assertAllClose(knp.where(x > 1, x, y), np.where(x > 1, x, y))
self.assertAllClose(knp.Where()(x > 1, x, y), np.where(x > 1, x, y))

def test_digitize(self):
x = np.array([0.0, 1.0, 3.0, 1.6])
bins = np.array([0.0, 3.0, 4.5, 7.0])
self.assertAllClose(knp.digitize(x, bins), np.digitize(x, bins))
abuelnasr0 marked this conversation as resolved.
Show resolved Hide resolved
self.assertAllClose(knp.Digitize()(x, bins), np.digitize(x, bins))
self.assertTrue(
standardize_dtype(knp.digitize(x, bins).dtype)
== standardize_dtype("int")
)
self.assertTrue(
standardize_dtype(knp.Digitize()(x, bins).dtype)
== standardize_dtype("int")
)

x = np.array([0.2, 6.4, 3.0, 1.6])
bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])
self.assertAllClose(knp.digitize(x, bins), np.digitize(x, bins))
self.assertAllClose(knp.Digitize()(x, bins), np.digitize(x, bins))
self.assertTrue(
standardize_dtype(knp.digitize(x, bins).dtype)
== standardize_dtype("int")
)
self.assertTrue(
standardize_dtype(knp.Digitize()(x, bins).dtype)
== standardize_dtype("int")
)

x = np.array([1, 4, 10, 15])
bins = np.array([4, 10, 14, 15])
self.assertAllClose(knp.digitize(x, bins), np.digitize(x, bins))
self.assertAllClose(knp.Digitize()(x, bins), np.digitize(x, bins))
self.assertTrue(
standardize_dtype(knp.digitize(x, bins).dtype)
== standardize_dtype("int")
)
self.assertTrue(
standardize_dtype(knp.Digitize()(x, bins).dtype)
== standardize_dtype("int")
)


class NumpyOneInputOpsCorrectnessTest(testing.TestCase):
def test_mean(self):
Expand Down