Using Tensorflow 1.4 I want to compute elementwise inverse (x --> 1/x) of a tensor using map functions. If the value of an element in the Tensor is zero, I want to have zero output.
As an example for the tensor: [[0, 1, 0], [0.5, 0.5, 0.3]]
, I want to have the output: [[0,1,0], [2, 2, 3.333]]
.
I know that I can easily get the desired output using tf.math.reciprocal_no_nan() in tf2.0
and tf.math.divide_no_nan()
in tf 1.4
, but I am wondering why the following code does not work:
tensor = tf.constant([[0, 1, 0], [0.5, 0.5, 0.3]], tf.float32)
tensor_inverse = tf.map_fn(lambda x: tf.cond(tf.math.not_equal(x, 0.0), lambda x: 1/x, lambda: 0) , tensor)
I get this error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()