Using tf.tensor as a Python 'bool' is not allowed

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.

The assignment says specifically to use the tf.greater function. I’ve tried multiplying function results by 1 and have also tried tf.cond.

Any suggestions on how to avoid this error?

Also, Coursera went down for maintenance in the middle of working in the lab this past Friday. Does anyone know how often this happens?

Thanks

Create a feature that shows if the traffic volume is greater than the mean and cast to an int

outputs[_transformed_name(_VOLUME_KEY)] = tf.cast(  
    
    # Use `tf.greater` to check if the traffic volume in a row is greater than the mean of the entire traffic volumn column
    tf.cond(tf.greater(traffic_volume, tf.metrics.Mean(traffic_volume)), lambda: 1, lambda: 0),
        tf.int64)

Hi @tbucci1 which week, assignment, exercise is this from?

regarding Coursera downtime, never happened before to me in more than 5 years, so you have been unfortunate but I would think it won’t happen soon.

hear you soon

Hi @tbucci1, I will asume that you are in C2-W2 assignment. Could you check the final utility of using tf.cond? Also, checking the official documentation of tf.greater.
regards,

Thanks - I think no one could possibly have my luck, naturally.

Yes, this is for Course2 Week2. I am posting in the correct forum, yes?

I’m not sure what you mean by ‘final utility’ using tf.cond?

I think tf.greater is working per the documentation, as it is clearly doing the comparison to create the boolean tensor.

The cast to tf.int64, however seems not to be working, I’m not sure why.

Any additional suggestions are appreciated.

Hi @tbucci1 , yes! you are posting in the right forum. I mean If you need to use tf.cond? And I think tf.int64 is ok. In this course these operations are quite common so running them correctly is going to help you in the forthcoming exercises. Hope it helps,
regards,

I wonder what is the reason for not allowing bool type? I’m surprised the instructions don’t mention this common situation.

I see quite a lot of work arounds posted online, including using @tf.function.

I’ve used these work arounds (*1, tf.cond, @tf.function) and they have not worked, the error is the same or similar depending on the work around.

It could be that the error is resulting by how I reference each row versus the value of the mean of the entire column. I wonder if there is an example of this in the instructions?

It appears as if using tft.mean instead of tf.metrics.Mean is helpful

1 Like

Hi @tbucci1 , I also think sometimes the instructions might have short examples of use. I will let staff know about this suggestion.

Sounds good, Jose, thanks. I hope it is helpful.

1 Like