How to raise an error based on condition in Tensorflow?

Viewed 83

I am writing a function my_function to be used as tf.data.Dataset.map argument, using tf.py_function, as recommended in the doc.

How can I raise a tf.errors.InvalidArgumentError in my_function ?

my_function

def my_function(data, param):
    _, n_freq_bins, n_time_bins, channels = tf.shape(data)
    tf.cond(
        param < n_time_bins,
        lambda: 1,
        <raise tf.errors.InvalidArgumentError>
    )
    ...

In my data pipeline:

dataset = dataset.map(lambda data, labels: (tf.py_function(my_function, [data, 100], tf.float32), labels))
0 Answers
Related