Skip to content

Fix TypeError in keras.ops.searchsorted by using shape instead of len #21472

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion keras/src/backend/tensorflow/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2134,7 +2134,7 @@ def searchsorted(sorted_sequence, values, side="left"):
f"sorted_sequence.shape={sorted_sequence.shape}"
)
out_type = (
"int32" if len(sorted_sequence) <= np.iinfo(np.int32).max else "int64"
"int32" if sorted_sequence.shape[0] <= np.iinfo(np.int32).max else "int64"
Copy link
Collaborator

Choose a reason for hiding this comment

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

You also need to check sorted_sequence.shape[0] is not None (it could be None for a symbolic tensor)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @fchollet, I have tested searchsorted with sorted_sequence as None and I am getting value error, traceback is shown here. Should I need to raise another value error ?

keras_sorted= keras.ops.searchsorted(None, [13, 12])
print("keras_sorted", keras_sorted)
Traceback (most recent call last):
  File "/Users/dhantule/Desktop/keras-test/searchsorted#21465.py", line 22, in <module>
    keras_sorted= keras.ops.searchsorted(None, [13, 12])
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dhantule/Desktop/dhantule-keras/keras/keras/src/ops/numpy.py", line 5329, in searchsorted
    sorted_sequence = backend.convert_to_tensor(sorted_sequence)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dhantule/Desktop/dhantule-keras/keras/keras/src/backend/tensorflow/core.py", line 153, in convert_to_tensor
    return tf.convert_to_tensor(x, dtype=dtype)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tensorflow/python/framework/constant_op.py", line 108, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh the sorted_sequence would not be None, simply, it's shape would have a None dimension.

I think this is one way to trigger it is:

keras_sorted= keras.ops.searchsorted(keras.KerasTensor((None,), dtype="float32"), [13, 12])

)
return tf.searchsorted(
sorted_sequence, values, side=side, out_type=out_type
Expand Down
Loading