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


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

TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。

polyval()用於計算多項式的元素明智值。

用法:tensorflow.math.polyval( coeffs, x, name)

參數:

  • coeffs:它是一個列表張量,表示多項式的係數。
  • x:它是代表多項式變量的張量。
  • name(optional):它定義了操作的名稱。

返回值:它返回一個張量。



如果coeffs是具有n個值的張量,而x是張量,則P(x)是n階多項式,其定義為:

p(x) = coeffs[n-1] + coeffs[n-2] * x + ... + coeffs[0] * x**(n-1)

範例1:

Python3

# importing the library 
import tensorflow as tf 
  
# Initializing the input tensor 
coeffs = [-1, 2, 3] 
x = tf.constant([7], dtype = tf.int32) 
  
# Printing the input tensor 
print('coeffs:', coeffs) 
print('x:', x) 
  
# Calculating Result 
res = tf.math.polyval(coeffs, x) 
  
# Printing the result 
print('Result:', res)

輸出:

coeffs: [-1, 2, 3]
x: tf.Tensor([7], shape=(1, ), dtype=int32)
Result: tf.Tensor([-32], shape=(1, ), dtype=int32)


範例2:

Python3

# importing the library 
import tensorflow as tf 
  
# Initializing the input tensor 
coeffs = [-1, 2, 3] 
x = tf.constant([7, 2], dtype = tf.int32) 
  
# Printing the input tensor 
print('coeffs:', coeffs) 
print('x:', x) 
  
# Calculating Result 
res = tf.math.polyval(coeffs, x) 
  
# Printing the result 
print('Result:', res)

輸出:

coeffs: [-1, 2, 3]
x: tf.Tensor([7 2], shape=(2, ), dtype=int32)
Result: tf.Tensor([-32   3], shape=(2, ), dtype=int32)




注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 Python – tensorflow.math.polyval()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。