Skip to content
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
4 changes: 2 additions & 2 deletions keras/src/backend/jax/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,10 @@ def imag(x):
return jnp.imag(x)


def isclose(x1, x2):
def isclose(x1, x2, rtol=1e-5, atol=1e-8, equal_nan=False):
x1 = convert_to_tensor(x1)
x2 = convert_to_tensor(x2)
return jnp.isclose(x1, x2)
return jnp.isclose(x1, x2, rtol, atol, equal_nan)


@sparse.densifying_unary
Expand Down
4 changes: 2 additions & 2 deletions keras/src/backend/numpy/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ def imag(x):
return np.imag(x)


def isclose(x1, x2):
return np.isclose(x1, x2)
def isclose(x1, x2, rtol=1e-5, atol=1e-8, equal_nan=False):
return np.isclose(x1, x2, rtol, atol, equal_nan)


def isfinite(x):
Expand Down
10 changes: 6 additions & 4 deletions keras/src/backend/tensorflow/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy as np
import tensorflow as tf
from tensorflow.python.ops.linalg.sparse import sparse_csr_matrix_ops
from tensorflow.python.ops.math_ops import is_nan

from keras.src import tree
from keras.src.backend import config
Expand Down Expand Up @@ -1250,16 +1251,17 @@ def imag(x):
return tf.math.imag(x)


def isclose(x1, x2):
def isclose(x1, x2, rtol=1e-5, atol=1e-8, equal_nan=False):
x1 = convert_to_tensor(x1)
x2 = convert_to_tensor(x2)
dtype = dtypes.result_type(x1.dtype, x2.dtype)
x1 = tf.cast(x1, dtype)
x2 = tf.cast(x2, dtype)
if "float" in dtype:
# atol defaults to 1e-08
# rtol defaults to 1e-05
return tf.abs(x1 - x2) <= (1e-08 + 1e-05 * tf.abs(x2))
result = tf.abs(x1 - x2) <= (atol + rtol * tf.abs(x2))
if equal_nan:
result = result | (is_nan(x1) & is_nan(x2))
return result
else:
return tf.equal(x1, x2)

Expand Down
4 changes: 2 additions & 2 deletions keras/src/backend/torch/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,13 +697,13 @@ def imag(x):
return torch.imag(x)


def isclose(x1, x2):
def isclose(x1, x2, rtol=1e-5, atol=1e-8, equal_nan=False):
x1 = convert_to_tensor(x1)
x2 = convert_to_tensor(x2)
result_dtype = dtypes.result_type(x1.dtype, x2.dtype)
x1 = cast(x1, result_dtype)
x2 = cast(x2, result_dtype)
return torch.isclose(x1, x2)
return torch.isclose(x1, x2, rtol, atol, equal_nan)


def isfinite(x):
Expand Down
17 changes: 11 additions & 6 deletions keras/src/ops/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2801,30 +2801,35 @@ def imag(x):


class Isclose(Operation):
def call(self, x1, x2):
return backend.numpy.isclose(x1, x2)
def call(self, x1, x2, rtol=1e-5, atol=1e-8, equal_nan=False):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change the signature in keras.ops, you need to change the backend function for all 4 backends, not just numpy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, silly mistake. I am still finding my way into the library. Fixed now!

return backend.numpy.isclose(x1, x2, rtol, atol, equal_nan)

def compute_output_spec(self, x1, x2):
def compute_output_spec(
self, x1, x2, rtol=1e-5, atol=1e-8, equal_nan=False
):
x1_shape = getattr(x1, "shape", [])
x2_shape = getattr(x2, "shape", [])
output_shape = broadcast_shapes(x1_shape, x2_shape)
return KerasTensor(output_shape, dtype="bool")


@keras_export(["keras.ops.isclose", "keras.ops.numpy.isclose"])
def isclose(x1, x2):
def isclose(x1, x2, rtol=1e-5, atol=1e-8, equal_nan=False):
"""Return whether two tensors are element-wise almost equal.

Args:
x1: First input tensor.
x2: Second input tensor.
rtol: Relative tolerance.
atol: Absolute tolerance.
equal_nan: If `True`, element-wise NaNs are considered equal.

Returns:
Output boolean tensor.
"""
if any_symbolic_tensors((x1, x2)):
return Isclose().symbolic_call(x1, x2)
return backend.numpy.isclose(x1, x2)
return Isclose().symbolic_call(x1, x2, rtol, atol, equal_nan)
return backend.numpy.isclose(x1, x2, rtol, atol, equal_nan)


class Isfinite(Operation):
Expand Down