2

I write a function in tensorflow 2, and use tf.keras to write a model. The function is defined below:

@tf.function
def mask_output(input_tensor,mask):
    if tf.math.count_nonzero(mask) > 0:
        output_tensor = tf.multiply(input_tensor, mask)
    else:
        output_tensor = input_tensor
    return output_tensor

The two parameters I give it is the tensor in the model. However, when I define the model, and call that function in the model definition, it says:

{_SymbolicException}Inputs to eager execution function cannot be Keras symbolic tensors, but found

[<tf.Tensor 'a_dense2/Identity:0' shape=(None, 12, 5) dtype=float32>, <tf.Tensor 'a_mask_input:0' shape=(None, 12, 5) dtype=float32>]

How to solve that? Why can't I call that funciton with two keras tensor input?

1
  • Hi @Rui Guo, can you provide a minimum reproducible code? Commented Apr 24, 2020 at 3:54

1 Answer 1

4

If running under eager mode, tensorflow operations will check if the inputs are of type tensorflow.python.framework.ops.EagerTensor and keras ops are implemented as DAGs. So if the inputs to the eager mode is of tensorflow.python.framework.ops.Tensor, then this throws the error.

You can change the input type to EagerTensor by explicity telling tensorflow to run in eager mode for keras by using tf.config.experimental_run_functions_eagerly(True). Adding this statement should solve your issue.

For example, this program throws the error you are facing -

Code to reproduce the error-

import numpy as np
import tensorflow as tf
print(tf.__version__)
from tensorflow.keras import layers, losses, models

def get_loss_fcn(w):
    def loss_fcn(y_true, y_pred):
        loss = w * losses.mse(y_true, y_pred)
        return loss
    return loss_fcn

data_x = np.random.rand(5, 4, 1)
data_w = np.random.rand(5, 4)
data_y = np.random.rand(5, 4, 1)

x = layers.Input([4, 1])
w = layers.Input([4])
y = layers.Activation('tanh')(x)
model = models.Model(inputs=[x, w], outputs=y)
loss = get_loss_fcn(model.input[1])

model.compile(loss=loss)
model.fit((data_x, data_w), data_y)

Output -

2.2.0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:

TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
  @tf.function
  def has_init_scope():
    my_constant = tf.constant(1.)
    with tf.init_scope():
      added = my_constant * 2
The graph tensor has name: input_8:0

During handling of the above exception, another exception occurred:

_SymbolicException                        Traceback (most recent call last)
8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     72       raise core._SymbolicException(
     73           "Inputs to eager execution function cannot be Keras symbolic "
---> 74           "tensors, but found {}".format(keras_symbolic_tensors))
     75     raise e
     76   # pylint: enable=protected-access

_SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'input_8:0' shape=(None, 4) dtype=float32>]

Solution - Adding this tf.config.experimental_run_functions_eagerly(True) at the top of the program runs the program successfully. Also adding tf.compat.v1.disable_eager_execution() at the top of the progrm to disable eager execution also runs the program successfully.

Fixed code -

import numpy as np
import tensorflow as tf
print(tf.__version__)
from tensorflow.keras import layers, losses, models

tf.config.experimental_run_functions_eagerly(True)

def get_loss_fcn(w):
    def loss_fcn(y_true, y_pred):
        loss = w * losses.mse(y_true, y_pred)
        return loss
    return loss_fcn

data_x = np.random.rand(5, 4, 1)
data_w = np.random.rand(5, 4)
data_y = np.random.rand(5, 4, 1)

x = layers.Input([4, 1])
w = layers.Input([4])
y = layers.Activation('tanh')(x)
model = models.Model(inputs=[x, w], outputs=y)
loss = get_loss_fcn(model.input[1])

model.compile(loss=loss)
model.fit((data_x, data_w), data_y)

print('Done.')

Output -

2.2.0
1/1 [==============================] - 0s 1ms/step - loss: 0.0000e+00
Done.

Hope this answers your question. Happy Learning.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for that answer. I'm confused about eager mode here. Is it in eager mode by default? With tf.config.experimental_run_functions_eagerly(True), the program will run in eager mode and if without it will not? Do you mean that error comes from giving the funcion inputs which are not tensorflow.python.framework.ops.Tensor or tensorflow.python.framework.ops.EagerTensor? What is the difference between tensorflow.python.framework.ops.Tensor and tensorflow.python.framework.ops.EagerTensor?
I modified the answer - "So if the inputs to the eager mode is of tensorflow.python.framework.ops.Tensor, then this throws the error". Error is because of tensorflow.python.framework.ops.Tensor and we need to change it to tensorflow.python.framework.ops.EagerTensor by using tf.config.experimental_run_functions_eagerly(True). Calling tf.config.experimental_run_functions_eagerly(True) will make all invocations of tf.function run eagerly instead of running as a traced graph function.
Being said that my program also ran fine when I used tf.compat.v1.disable_eager_execution() instead of tf.config.experimental_run_functions_eagerly(True). In this cases the tensors are converted the other way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.