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

Chapter 11 Part2 CodeList 11-12 tf.one_hot() issue #239

Open
boyfzb2018 opened this issue May 16, 2024 · 2 comments
Open

Chapter 11 Part2 CodeList 11-12 tf.one_hot() issue #239

boyfzb2018 opened this issue May 16, 2024 · 2 comments

Comments

@boyfzb2018
Copy link

boyfzb2018 commented May 16, 2024

I tried to run this file https://github.com/fchollet/deep-learning-with-python-notebooks/blob/master/chapter11_part02_sequence-models.ipynb and this section as the below:

import tensorflow as tf
inputs = keras.Input(shape=(None,), dtype="int64")
embedded = tf.one_hot(inputs, depth=max_tokens)
x = layers.Bidirectional(layers.LSTM(32))(embedded)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(1, activation="sigmoid")(x)
model = keras.Model(inputs, outputs)
model.compile(optimizer="rmsprop",
loss="binary_crossentropy",
metrics=["accuracy"])
model.summary()

But I am getting the following error:

/opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/framework/op_def_library.py in _ExtractInputsAndAttrs(op_type_name, op_def, allowed_list_attr_map, keywords, default_type_attr_map, attrs, inputs, input_types)
570 values, as_ref=input_arg.is_ref).dtype.name
571 except ValueError as err:
--> 572 raise ValueError(
573 f"Tried to convert '{input_name}' to a tensor and failed. "
574 f"Error: {err}")

ValueError: Tried to convert 'indices' to a tensor and failed. Error: A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces keras.layers and keras.operations). You are likely doing something like:

x = Input(...)
...
tf_fn(x) # Invalid.

Can someone help me with this?
Thanks!

@ifond
Copy link

ifond commented May 16, 2024 via email

@oabuhamdan
Copy link

You can do one of two:

First solution

inputs = keras.Input(shape=(500, ), dtype="int64")
one_hot = tf.keras.layers.Lambda(lambda x: tf.one_hot(x, depth=max_tokens), output_shape=(500, max_tokens))(inputs)
x = keras.layers.Bidirectional(keras.layers.LSTM(32))(one_hot)
x = keras.layers.Dropout(0.5)(x)
outputs = keras.layers.Dense(1, activation="sigmoid")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer="rmsprop", loss="binary_crossentropy", metrics=["accuracy"])
model.summary()

Second Solution

text_vectorization = keras.layers.TextVectorization(
    # output_sequence_length=max_length, this can be used with output mode other than int. You can keep it as is or truncate it manually
    output_mode="one_hot",
    max_tokens=max_tokens
)

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

No branches or pull requests

3 participants