Python ``tensorflow.math.is_nan()`` 函数与实例


发布日期 : 2024-06-15 01:59:35 UTC

访问量: 10 次浏览

Python – tensorflow.math.is\_nan()

TensorFlow是谷歌设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

is\_nan()
如果元素是NaN则返回true,否则返回false。

语法:
tensorflow.math.is_NaN( x, name)

参数:

  • x:它是一个张量。允许的d类型是bfloat16, half, float32, float64。
  • name(可选):它定义了该操作的名称

返回:
它返回一个dtype bool的张量。

示例 1:

# importing the library
import tensorflow as tf
import numpy as np

# Initializing the input tensor
a = tf.constant([7, 8, 13, 11, np.inf], dtype = tf.float64)

# Printing the input tensor
print('a: ', a)

# Calculating the result
res = tf.math.is_nan(a)

# Printing the result
print('Result: ', res)

输出:

a: tf.Tensor([ 7. 8. 13. 11. inf], shape=(5, ), dtype=float64)
Result: tf.Tensor([False False False False False], shape=(5, ), dtype=bool)

实例2:
本实例使用numpy nan。

# Importing the libraray
import tensorflow as tf
import numpy as np

# Initializing the input tensor
a = tf.constant([7, 8, 13, 11, np.nan], dtype = tf.float64)

# Printing the input tensor
print('a: ', a)

# Calculating the result
res = tf.math.is_nan(a)

# Printing the result
print('Result: ', res)

输出:

a: tf.Tensor([ 7. 8. 13. 11. nan], shape=(5, ), dtype=float64)
Result: tf.Tensor([False False False False True], shape=(5, ), dtype=bool)