keras icon indicating copy to clipboard operation
keras copied to clipboard

This works in keras 2.15 with TensorFlow 2.15 but does not 3.5 with TensorFlow 2.17: multiple lookup layer types in inputs

Open jasonbrancazio opened this issue 1 year ago • 5 comments
trafficstars

In Keras 3, calling a model specified via the functional API with one StringLookupLayer and one IntegerLookupLayer each connected to their own Input layers raises a "Cast int64 to string" error, but in Keras 2, the tensors flow.

Here is a minimal example to reproduce the error. Try running it first in the latest version of Keras and TensorFlow, see the error, then downgrade and observe that it works:

import keras
import numpy as np
import tensorflow as tf

# Create a string lookup layer
data = [["a", "c", "d"], ["d", "z", "b"]]
letters_lookup = keras.layers.StringLookup(name='letters_lookup', output_mode='one_hot')
letters_lookup.adapt(data)

# Create an integer lookup layer
data = np.array([[12, 1138, 42], [42, 1000, 36]])
integers_lookup = keras.layers.IntegerLookup(name='integers_lookup', output_mode='one_hot')
integers_lookup.adapt(data)

# Create input layers
integers_input = keras.Input(shape=(), name='integers_in', dtype="int64")
letters_input = keras.Input(shape=(), name='letters_in', dtype="string")

# Make a model using the functional API
letters_out = letters_lookup(letters_input)
integers_out = integers_lookup(integers_input)
model = keras.Model([letters_input,integers_input],[letters_out,integers_out])

# Test the model
test_tensor_dict = {
    'letters_in': tf.constant(['d']),
    'integers_in': tf.constant([42], dtype=tf.int64)
}
model(test_tensor_dict)
2024-08-26 17:50:26.960422: W tensorflow/core/framework/op_kernel.cc:1817] OP_REQUIRES failed at cast_op.cc:122 : UNIMPLEMENTED: Cast int64 to string is not supported
2024-08-26 17:50:26.960536: I tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: UNIMPLEMENTED: Cast int64 to string is not supported
---------------------------------------------------------------------------
UnimplementedError                        Traceback (most recent call last)
Cell In[14], line 1
----> 1 model(test_tensor_dict)

File /opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    119     filtered_tb = _process_traceback_frames(e.__traceback__)
    120     # To get the full stack trace, call:
    121     # `keras.config.disable_traceback_filtering()`
--> 122     raise e.with_traceback(filtered_tb) from None
    123 finally:
    124     del filtered_tb

File /opt/conda/lib/python3.10/site-packages/tensorflow/python/framework/ops.py:5983, in raise_from_not_ok_status(e, name)
   5981 def raise_from_not_ok_status(e, name) -> NoReturn:
   5982   e.message += (" name: " + str(name if name is not None else ""))
-> 5983   raise core._status_to_exception(e) from None

UnimplementedError: Exception encountered when calling Functional.call().

{{function_node __wrapped__Cast_device_/job:localhost/replica:0/task:0/device:CPU:0}} Cast int64 to string is not supported [Op:Cast] name: 

Arguments received by Functional.call():
  • inputs={'letters_in': 'tf.Tensor(shape=(1,), dtype=string)', 'integers_in': 'tf.Tensor(shape=(1,), dtype=int64)'}
  • training=None
  • mask={'letters_in': 'None', 'integers_in': 'None'}

Tested in Colab and also on a Mac laptop running JupyterLab in a Google Cloud Deep Learning container.

jasonbrancazio avatar Aug 26 '24 17:08 jasonbrancazio

Your code runs fine if you define the model like this:

model = keras.Model(
    {"letters_in": letters_input, "integers_in": integers_input},  # Should match the inputs structure.
    [letters_out, integers_out],
)

I have proposed a PR to warn users about this.

james77777778 avatar Aug 27 '24 07:08 james77777778

@jasonbrancazio , Please find the attached working Gist with the above suggested changes , please verify and let us know if you need any further inputs. Thanks!

sachinprasadhs avatar Oct 03 '24 18:10 sachinprasadhs

thanks for the suggestion. no further input needed.

jasonbrancazio avatar Oct 04 '24 00:10 jasonbrancazio

Feel free to close the issue as well. Thank you.

sachinprasadhs avatar Oct 08 '24 18:10 sachinprasadhs

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] avatar Oct 23 '24 02:10 github-actions[bot]

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] avatar Dec 04 '24 02:12 github-actions[bot]

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

github-actions[bot] avatar Dec 19 '24 02:12 github-actions[bot]

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar Dec 19 '24 02:12 google-ml-butler[bot]