fchollet / deep-learning-with-python-notebooks

Jupyter notebooks for the code samples of the book "Deep Learning with Python"
MIT License
17.95k stars 8.48k forks source link

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

Open boyfzb2018 opened 1 month ago

boyfzb2018 commented 1 month ago

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 commented 1 month ago

​ I have received your E-mail——Steven Lee