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
19 changes: 18 additions & 1 deletion keras/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
convert_to_tensor
convert_to_numpy
cond
is_tensor
"""

import numpy as np
Expand Down Expand Up @@ -402,7 +403,7 @@ def unstack(x, num=None, axis=0):
def shape(x):
"""Gets the shape of the tensor input.

Note: On the tensorflow backend, when `x` is a `tf.Tensor` with dynamic
Note: On the TensorFlow backend, when `x` is a `tf.Tensor` with dynamic
shape, dimensions which are dynamic in the context of a compiled function
will have a `tf.Tensor` value instead of a static integer value.

Expand Down Expand Up @@ -629,3 +630,19 @@ def vectorized_map(function, elements)
a single list of tensor arguments.
"""
return backend.core.vectorized_map(function, elements)


@keras_export("keras.ops.is_tensor")
def is_tensor(x):
"""Check whether the given object is a tensor.

Note: This checks for backend specific tensors so passing a TensorFlow
tensor would return `False` if your backend is PyTorch or JAX.

Args:
x: A variable.

Returns:
`True` if `x` is a tensor, otherwise `False`.
"""
return backend.core.is_tensor(x)
8 changes: 8 additions & 0 deletions keras/ops/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,11 @@ def fn(elems):
self.assertAllClose(
backend.convert_to_numpy(output), 2 * np.ones((2, 3))
)

def test_is_tensor(self):
np_x = np.array([[1, 2, 3], [3, 2, 1]])
x = backend.convert_to_tensor(np_x)
if backend.backend() != "numpy":
self.assertFalse(ops.is_tensor(np_x))
self.assertTrue(ops.is_tensor(x))
self.assertFalse(ops.is_tensor([1, 2, 3]))