OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed

In week 3, the grading assignment of reinforcement learning
I try to run this block of code


But I run into this problem:

Can anyone help me.

My first guess is maybe there is an error in your compute_loss() function.

Or, perhaps you deleted some code you should not have touched?
image

The code is original,


I actually have submit the assignment and it still received 100% score

That’s very interesting.

Hello @thaison23092003

The autograder is not able to check everything, but if you are concerned about the error, the first thing I would do if I were you would be to google the meaning of OperatorNotAllowedInGraphError.

According to Tensorflow documentation, OperatorNotAllowedInGraphError is “raised when an unsupported operator is present in Graph execution.”

Then reading the error again, seems that it has something to do with that line.

While what operators are supported change over TF versions, the safest (and perhaps most beautiful) way is to implement everything arthimetrically (using + - * / ) whenever possible. The hint underneath the graded code cell for compute_loss tells you how to do that. Following the hint should get rid of that error.

If you wanted to use conditional statement, sum and comparator, and you were familiar with tensorflow, then you might consider tensorflow’s native functions first, such as tf.cond, tf.math.reduce_sum, and tf.math.equal. If you are able to replace those Python native operators with Tensorflow’s, that error should go away too, but I will leave it to you to explore :wink:

Cheers,
Raymond

Good points, Raymond.

Here’s the hint:

It doesn’t mention using sum() on done_vals.