OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function

C4 - Week 1 Assignment 2
This is my code:

{moderator edit - solution code removed}


Why am I getting this error message?

Please post the entire error message. The part you posted doesn’t include the call to your code.

Problem Solved.
Thanks anyways!

Hello! Can you please explain how you solved the error? I am encountering it too.

1 Like

You can see the bug in the code that @priyamthakkar8 posted above. Notice that the first call to ReLU above that one is different. The syntax is pretty confusing here and they don’t do a good job of explaining it, but you are doing everything in two steps here:

  1. You call a TFL function to “instantiate” it with a given set of parameters.
  2. Then you “invoke” that function with an actual tensor as input and it outputs a tensor.

That’s why you see two sets of parens in the earlier correct expression:

The ReLU() is the “instantiation” step. That gives you back a function and then you invoke it with the input Z1.

Here is a great Discourse post from Ai_Curious which explains the Keras Sequential and Functional APIs. Way more useful than the minimal explanations in the course materials.