I want to raise a tf.errors.InvalidArgumentError
exception dependent on the value of an input tensor in graph mode (in TensorFlow serving).
Currently I use tf.debugging.assert_all_finite
and this works fine. As I'm not making an assertion to bug check, but raising an exception based on the input, it would be better to raise an explicit exception.
My question boils down to:
- How to conditionally execute code that doesn't return a tensor
- How to raise a tf.errors exception.
What is the proper way of doing this?
Edit: Some more detail. I would like to recreate the following logic without using tf.debugging (unless that is in fact the correct way to do it).
Currently I am checking that there aren't NaN
values like this:
assert_op = tf.debugging.assert_all_finite(
input_data,
'Cant have nans at beginning or end'
)
tf.cond
which can be used to emulate that behavior. Note thattf.errors.InvalidArgumentError
is raised at compile time, not at runtime. – Scarpertf.Assert
. You cannot catch it (within the graph), though. I have expanded upon that in my answer. – Aesthetictf.Assert
usestf.cond
under the covers to create the logical branches. The exception is then raised in the "controller" space. Maybe I misunderstood the question but it appeared that the goal was to raise an exception during runtime. This is quite different from having tensorflow raise an exception - not with respect to the result but regarding the implementation. Nevertheless I agree thattf.Assert
is exactly the right option here. – Scarper