tf.math.erfinv should outputs NaN instead of inf when receiving an invalid input.
Hi, @maybeLee 

I was able to replicate the issue on Google Colab and I tried with latest stable version of Tensorflow `2.11` and also tried with `tf-nightly-2.13.0-dev20230207` but it's returning `inf` and for your reference I have added [gist file](https://colab.research.google.com/gist/gaikwadrahul8/dafdf6ccd4560cc198c313fe9ad78551/-59576.ipynb) so it seems like we have to dig more into this issue, we will update soon here and thank you for noticing the issue. Thank you! Hi, @gaikwadrahul8 

I confirm your reproduced result. Following the MatLab's documentation: https://www.mathworks.com/help/matlab/ref/erfinv.html, PyTorch and Scipy's result, I think `tf.math.erfinv` outputs NaN instead of `inf` would be better. The reason is that: `inf` may be converted to other normal values (e.g., `tf.math.reciprocal(tf.math.erfinv(1.1))=0.0`), then the **problem (i.e., tf.math.erfinv receives an invalid input and its output should not be relied on) is hidden.** 

Instead, if erfinv outputs NaN, the problem will not be hidden (e.g., `tf.math.reciprocal(tf.math.erfinv(1.1)=NaN`, which is my expectation when using the tf.math.erfinv). Hi, @sachinprasadhs 

Could you please look into this issue? Thank you! TensorFlow's behavior with the erfinv function differs from other libraries like PyTorch and SciPy when handling invalid input values.

To work around this issue and achieve consistent behavior across libraries, you can use tf.where along with tf.math.is_finite to replace Inf values with NaN. Here's an example:
```
import tensorflow as tf

def erfinv_with_nan(input_tensor):
    erfinv_result = tf.math.erfinv(input_tensor)
    return tf.where(tf.math.is_finite(erfinv_result), erfinv_result, float('nan'))

input_value = tf.Variable(1.1)
result = erfinv_with_nan(input_value)
print(result)

```
This will output below
`tf.Tensor(nan, shape=(), dtype=float32)`

This way, you can ensure that the output is consistent with the behavior observed in other libraries like PyTorch and SciPy.

Please note that this is a workaround and not a fix for the inconsistency in TensorFlow's implementation of erfinv. You can consider raising an issue on TensorFlow's GitHub repository to bring it to the developers' attention. This should now be fixed with the latest Eigen update. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/59576">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/59576">No</a>

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

Yes

### Source

source

### Tensorflow Version

2.9.2, 2.11.0

### Custom Code

Yes

### OS Platform and Distribution

_No response_

### Mobile device

_No response_

### Python version

3.9, 3.8

### Bazel version

N/A

### GCC/Compiler version

N/A

### CUDA/cuDNN version

N/A

### GPU model and memory

N/A

### Current Behaviour?

```shell
The valid input range of `erfinv` is [-1, 1] and inputs outside of this range are invalid inputs. For these invalid inputs, [Matlab](https://www.mathworks.com/help/matlab/ref/erfinv.html), spicy, and pytorch output NaN. The current TF version outputs Inf. Here is the colab link to reproduce: https://colab.research.google.com/drive/10J4sDIi07sQ2WILXrVnmO3eMo7Kld-XY?usp=sharing
```


### Standalone code to reproduce the issue

```shell
import tensorflow as tf
print(tf.math.erfinv(tf.Variable(1.1)))
```


### Relevant log output

```shell
TF: tf.Tensor(inf, shape=(), dtype=float32)
torch: tensor(nan)
spicy: nan
```
</details>