简体   繁体   中英

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).

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:

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. Any idea how I can rectify this?

Using the Code Snippet you provided and adding basic Keras Binary Classification 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 .
  2. Check your Input Data Shapes and Type , make sure that it is compatible with the layers used. (Tensor Datatype as much as possible)

Hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM