Skip to content

Commit 232cf26

Browse files
authored
Implement isreal function in keras.ops (#21740)
* Add initial version * Add tensorflow version * Update numpy.py for ops * Update numpy_test.py * Add method for openvino * clean code * update code by gemini review * update test case for non-complex type
1 parent ccbc9d4 commit 232cf26

File tree

12 files changed

+95
-0
lines changed

12 files changed

+95
-0
lines changed

keras/api/_tf_keras/keras/ops/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
from keras.src.ops.numpy import isnan as isnan
210210
from keras.src.ops.numpy import isneginf as isneginf
211211
from keras.src.ops.numpy import isposinf as isposinf
212+
from keras.src.ops.numpy import isreal as isreal
212213
from keras.src.ops.numpy import kaiser as kaiser
213214
from keras.src.ops.numpy import kron as kron
214215
from keras.src.ops.numpy import lcm as lcm

keras/api/_tf_keras/keras/ops/numpy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
from keras.src.ops.numpy import isnan as isnan
9696
from keras.src.ops.numpy import isneginf as isneginf
9797
from keras.src.ops.numpy import isposinf as isposinf
98+
from keras.src.ops.numpy import isreal as isreal
9899
from keras.src.ops.numpy import kaiser as kaiser
99100
from keras.src.ops.numpy import kron as kron
100101
from keras.src.ops.numpy import lcm as lcm

keras/api/ops/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
from keras.src.ops.numpy import isnan as isnan
210210
from keras.src.ops.numpy import isneginf as isneginf
211211
from keras.src.ops.numpy import isposinf as isposinf
212+
from keras.src.ops.numpy import isreal as isreal
212213
from keras.src.ops.numpy import kaiser as kaiser
213214
from keras.src.ops.numpy import kron as kron
214215
from keras.src.ops.numpy import lcm as lcm

keras/api/ops/numpy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
from keras.src.ops.numpy import isnan as isnan
9696
from keras.src.ops.numpy import isneginf as isneginf
9797
from keras.src.ops.numpy import isposinf as isposinf
98+
from keras.src.ops.numpy import isreal as isreal
9899
from keras.src.ops.numpy import kaiser as kaiser
99100
from keras.src.ops.numpy import kron as kron
100101
from keras.src.ops.numpy import lcm as lcm

keras/src/backend/jax/numpy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,11 @@ def isposinf(x):
819819
return jnp.isposinf(x)
820820

821821

822+
def isreal(x):
823+
x = convert_to_tensor(x)
824+
return jnp.isreal(x)
825+
826+
822827
def kron(x1, x2):
823828
x1 = convert_to_tensor(x1)
824829
x2 = convert_to_tensor(x2)

keras/src/backend/numpy/numpy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,11 @@ def isposinf(x):
745745
return np.isposinf(x)
746746

747747

748+
def isreal(x):
749+
x = convert_to_tensor(x)
750+
return np.isreal(x)
751+
752+
748753
def kron(x1, x2):
749754
x1 = convert_to_tensor(x1)
750755
x2 = convert_to_tensor(x2)

keras/src/backend/openvino/excluded_concrete_tests.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ NumpyDtypeTest::test_isin
3535
NumpyDtypeTest::test_isinf
3636
NumpyDtypeTest::test_isnan
3737
NumpyDtypeTest::test_isposinf
38+
NumpyDtypeTest::test_isreal
3839
NumpyDtypeTest::test_kron
3940
NumpyDtypeTest::test_lcm
4041
NumpyDtypeTest::test_logaddexp2
@@ -90,6 +91,7 @@ NumpyOneInputOpsCorrectnessTest::test_imag
9091
NumpyOneInputOpsCorrectnessTest::test_isfinite
9192
NumpyOneInputOpsCorrectnessTest::test_isinf
9293
NumpyOneInputOpsCorrectnessTest::test_isposinf
94+
NumpyOneInputOpsCorrectnessTest::test_isreal
9395
NumpyOneInputOpsCorrectnessTest::test_logaddexp2
9496
NumpyOneInputOpsCorrectnessTest::test_max
9597
NumpyOneInputOpsCorrectnessTest::test_mean
@@ -149,10 +151,12 @@ NumpyOneInputOpsDynamicShapeTest::test_corrcoef
149151
NumpyOneInputOpsDynamicShapeTest::test_hamming
150152
NumpyOneInputOpsDynamicShapeTest::test_hanning
151153
NumpyOneInputOpsDynamicShapeTest::test_isposinf
154+
NumpyOneInputOpsDynamicShapeTest::test_isreal
152155
NumpyOneInputOpsDynamicShapeTest::test_kaiser
153156
NumpyOneInputOpsStaticShapeTest::test_angle
154157
NumpyOneInputOpsStaticShapeTest::test_cbrt
155158
NumpyOneInputOpsStaticShapeTest::test_isposinf
159+
NumpyOneInputOpsStaticShapeTest::test_isreal
156160
NumpyTwoInputOpsDynamicShapeTest::test_gcd
157161
NumpyTwoInputOpsDynamicShapeTest::test_heaviside
158162
NumpyTwoInputOpsDynamicShapeTest::test_hypot

keras/src/backend/openvino/numpy.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,10 @@ def isposinf(x):
10031003
)
10041004

10051005

1006+
def isreal(x):
1007+
raise NotImplementedError("`isreal` is not supported with openvino backend")
1008+
1009+
10061010
def kron(x1, x2):
10071011
raise NotImplementedError("`kron` is not supported with openvino backend")
10081012

keras/src/backend/tensorflow/numpy.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,14 @@ def isposinf(x):
17121712
return tf.math.equal(x, tf.constant(float("inf"), dtype=x.dtype))
17131713

17141714

1715+
def isreal(x):
1716+
x = convert_to_tensor(x)
1717+
if x.dtype.is_complex:
1718+
return tf.equal(tf.math.imag(x), 0)
1719+
else:
1720+
return tf.ones_like(x, dtype=tf.bool)
1721+
1722+
17151723
def kron(x1, x2):
17161724
x1 = convert_to_tensor(x1)
17171725
x2 = convert_to_tensor(x2)

keras/src/backend/torch/numpy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,11 @@ def isposinf(x):
946946
return torch.isposinf(x)
947947

948948

949+
def isreal(x):
950+
x = convert_to_tensor(x)
951+
return torch.isreal(x)
952+
953+
949954
def kron(x1, x2):
950955
x1 = convert_to_tensor(x1)
951956
x2 = convert_to_tensor(x2)

0 commit comments

Comments
 (0)