Skip to content
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

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 #20166

Open
jasonbrancazio opened this issue Aug 26, 2024 · 1 comment
Assignees
Labels

Comments

@jasonbrancazio
Copy link

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.

@james77777778
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

11 participants
@james77777778 @jasonbrancazio @mehtamansi29 and others