简体   繁体   English

急切执行 function 的输入不能是 Keras 符号张量,但可以找到

[英]Inputs to eager execution function cannot be Keras symbolic tensors, but found

I'm trying to obtain the jacobian of my model wrt an input (sample_x which is a binary vector in numpy).我正在尝试获取我的 model wrt 输入的雅可比(sample_x 是 numpy 中的二进制向量)。

print("Initiating gradient checker")
    sample_x_tensor = sample_x.toarray()
    sample_x_tensor = tf.convert_to_tensor(sample_x.toarray())
    sample_x_tensor = tf.cast(sample_x_tensor, tf.float32)

    with tf.GradientTape() as tape:
        tape.watch(sample_x_tensor)
        y_pred = model(sample_x_tensor)
        print(y_pred[0])

    jacobian = tape.jacobian(y_pred, sample_x_tensor)

Model is a straightforward Keras binary classification model, Keras 2.15 and Tensorflow 2. Getting the following exception: Model is a straightforward Keras binary classification model, Keras 2.15 and Tensorflow 2. Getting the following exception:

tensorflow.python.eager.core._SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'Reshape:0' 
shape=(1,) dtype=float32>]

To my understanding, TF2 has eager execution by default.据我了解,TF2 默认具有急切执行。 Any idea how I can rectify this?知道如何纠正这个问题吗?

Using the Code Snippet you provided and adding basic Keras Binary Classification Model.使用您提供的代码片段并添加基本的 Keras 二进制分类Model。

Code used for reproduction:用于复制的代码:

%tensorflow_version 2.x  # Using Google Colab

import tensorflow as tf  # Tensorflow 2.2.0-rc3
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

model = Sequential()
model.add(Dense(32, input_shape = (2,)))
model.add(Dense(1, activation = 'sigmoid'))

print("Initiating gradient checker")
sample_x_tensor = tf.random.normal((100,2))
# sample_x_tensor = sample_x.toarray()
# sample_x_tensor = tf.convert_to_tensor(sample_x.toarray())
sample_x_tensor = tf.cast(sample_x_tensor, tf.float32)

with tf.GradientTape() as tape:
    tape.watch(sample_x_tensor)
    y_pred = model(sample_x_tensor)
    print(y_pred[0])

jacobian = tape.jacobian(y_pred, sample_x_tensor)

This returns:这将返回:

Initiating gradient checker
tf.Tensor([0.38266268], shape=(1,), dtype=float32)

It executed successfully, but I would suggest a few things for you:它执行成功,但我会为您建议一些事情:

  1. Check your Model Layers and see if it is processing the right data types .检查您的 Model,看看它是否处理了正确的数据类型
  2. Check your Input Data Shapes and Type , make sure that it is compatible with the layers used.检查您的输入数据形状类型,确保它与使用的图层兼容。 (Tensor Datatype as much as possible) (尽可能使用Tensor Datatype)

Hope this helps you.希望这对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 急切执行函数的输入不能是 Keras 符号张量 - Inputs to eager execution function cannot be Keras symbolic tensors 自定义损失问题:急切执行的输入 function 不能是 keras 符号张量但找到 - Custom loss problem: inputs to eager execution function cannot be keras symbolic tensors but found 在 Keras 中训练变分自动编码器引发“SymbolicException:急切执行 function 的输入不能是 Keras 符号张量” - Training Variational Autoencoder in Keras raises “SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors” 无法将数据输入自定义损失:急切执行 function 的输入不能是 Keras 符号张量 - Can't input data to custom loss: Inputs to eager execution function cannot be Keras symbolic tensors tf.keras 两个损失,中间层作为其中一个的输入错误:急切执行的输入 function 不能是 Keras 符号张量 - tf.keras two losses, with intermediate layers as input to of one of them error:Inputs to eager execution function cannot be Keras symbolic tensors 自定义损失 function Tensorflow 2 [Keras 符号输入/输出未实现] - Custom loss function Tensorflow 2 [Keras symbolic inputs/outputs do not implement] 在Keras中使用2个张量输入定义自定义层 - Define a custom layer with 2 tensors inputs in Keras 使用 Tensorflow 2.0 和没有 Keras 的急切执行 - Using Tensorflow 2.0 and eager execution without Keras Keras 的 TensorFlow 后端是否依赖于 Eager Execution? - Does the TensorFlow backend of Keras rely on the eager execution? 急于执行时,Tensor对象不可迭代…使用Keras形状函数时 - Tensor Objects are not iterable when eager execution… while using Keras shape function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM