TypeError: You are passing KerasTensor

错误信息:TypeError: You are passing KerasTensor(type_spec=TensorSpec(shape=(), dtype=tf.float32, name=None), name='Placeholder:0', description="created by layer 'tf.cast_2'"), an intermediate Keras symbolic input/output, to a TF API that does not allow registering custom dispatchers, such as `tf.cond`, `tf.function`, gradient tapes, or `tf.map_fn`. Keras Functional model construction only supports TF API calls that *do* support dispatching, such as `tf.math.add` or `tf.reshape`. Other APIs cannot be called directly on symbolic Kerasinputs/outputs. You can work around this limitation by putting the operation in a custom Keras layer `call` and calling that layer on this symbolic input/output.

解释一下:

这个错误可能是由于你正在使用KerasTensor类型的输入或输出,并尝试将其传递给不支持自定义分发器(如tf.cond、tf.function或tf.map_fn)的TensorFlow API导致的。

要解决此问题,您可以考虑将该操作放入自定义Keras层的call()函数中,并在此层上调用该层的类别,而不是直接使用该函数。这样,您就可以通过输入/输出来执行该操作。

更改操作

在开始两行加入

  1. from tensorflow.python.framework.ops import disable_eager_execution
    
    disable_eager_execution()

即可。 tensorflow 2.12版本   Keras 2.12版本

觉得有用请点个赞支持一下

你可能感兴趣的:(python,深度学习,python,tensorflow)