📜  Python – tensorflow.math.is_finite()

📅  最后修改于: 2022-05-13 01:55:02.485000             🧑  作者: Mango

Python – tensorflow.math.is_finite()

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

is_finite()用于检查 x 的元素有限性。

示例 1:此示例使用 numpy inf 并检查它的有限性。

Python3
# 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_finite(a)
 
# Printing the result
print('Result: ', res)


Python3
# Importing the library
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_finite(a)
 
# Printing the result
print('Result: ', res)


输出:

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

示例 2:此示例使用 numpy nan 并检查它的有限性。

Python3

# Importing the library
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_finite(a)
 
# Printing the result
print('Result: ', res)

输出:

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