TensorFlow是Google设计的开源python库,用于开发机器学习模型和深度学习神经网络。 nextafter()用于在x2方向上查找元素wisenext可表示的x1值。
用法:tf.math.nextafter(x1, x2, name)
参数:
- x1:它是输入张量。此张量的允许dtype为float64,float32。
- x2:dtype与x1相同的输入张量。
- name(optional):它定义了操作的名称。
返回值:
它返回dtype的张量x1。
范例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)
输出:
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]
相关用法
注:本文由纯净天空筛选整理自aman neekhara大神的英文原创作品 Python – tensorflow.math.nextafter()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。