Chapter 11 Part2 CodeList 11-12 tf.one_hot() issue
boyfzb2018 opened this issue · comments
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!
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
)