當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tensorflow.math.nextafter()用法及代碼示例

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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。