Python – tensorflow.math.nextafter()
TensorFlow是谷歌设计的开源python库,用于开发机器学习模型和深度学习神经网络。 nextafter()用于在x2的方向上找到x1的wisenext可表示的元素。
语法: tf.math.nextafter(x1, x2, name)
参数 :
- x1:这是输入张量。这个张量允许的dtype是float64, float32。
- x2:它是与x1相同d类型的输入张量。
- name(可选):它定义了操作的名称。
返回值:
它返回一个dtype的张量为x1。
示例 1:
# 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)
输出:
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。
# 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]