📜  Python – tensorflow.math.nextafter()

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

Python – tensorflow.math.nextafter()

TensorFlow 是由谷歌设计的开源Python库,用于开发机器学习模型和深度学习神经网络。 nextafter()用于在 x2 的方向上查找 x1 的元素 wisenext 可表示值。

示例 1:

Python3
# Importing the library
import tensorflow as tf
  
# Initializing the input tensor
x1 = tf.constant([1, 2, -3, -4], dtype = tf.float64)
x2 = tf.constant([5, -7, 3, -8], dtype = tf.float64)
  
# Printing the input tensor
print('x1: ', x1)
print('x2: ', x2)
  
# Calculating result
res = tf.math.nextafter(x1, x2)
  
# Printing the result
print('Result: ', res)


Python3
# importing the library
import tensorflow as tf
  
# Initializing the input tensor
x1 = tf.constant([1, 2, -3, -4], dtype = tf.float64)
x2 = tf.constant([5, -7, 3, -8], dtype = tf.float32)
  
# Printing the input tensor
print('x1: ', x1)
print('x2: ', x2)
  
# Calculating result
res = tf.math.nextafter(x1, x2)
  
# Printing the result
print('Result: ', res)


输出:

x1:  tf.Tensor([ 1.  2. -3. -4.], shape=(4, ), dtype=float64)
x2:  tf.Tensor([ 5. -7.  3. -8.], shape=(4, ), dtype=float64)
Result:  tf.Tensor([ 1.  2. -3. -4.], shape=(4, ), dtype=float64)


示例 2:此示例对 x1 和 x2 使用不同的 dtype。它将引发 InvalidArgumentError。

Python3

# importing the library
import tensorflow as tf
  
# Initializing the input tensor
x1 = tf.constant([1, 2, -3, -4], dtype = tf.float64)
x2 = tf.constant([5, -7, 3, -8], dtype = tf.float32)
  
# Printing the input tensor
print('x1: ', x1)
print('x2: ', x2)
  
# Calculating result
res = tf.math.nextafter(x1, x2)
  
# Printing the result
print('Result: ', res)

输出:

x1:  tf.Tensor([ 1.  2. -3. -4.], shape=(4, ), dtype=float64)
x2:  tf.Tensor([ 5. -7.  3. -8.], shape=(4, ), dtype=float32)

---------------------------------------------------------------------------

InvalidArgumentError                      Traceback (most recent call last)

 in ()
      8 
      9 # Calculating result
---> 10 res = tf.math.nextafter(x1, x2)
     11 
     12 # Printing the result

2 frames

/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: cannot compute NextAfter as input #1(zero-based) was expected to be a double tensor but is a float tensor [Op:NextAfter]