Python TensorFlow:``tensorflow.math.polyval()`` 多项式求值函数详解


发布日期 : 2019-06-06 08:39:19 UTC

访问量: 10 次浏览

Python – tensorflow.math.polyval()

TensorFlow是谷歌设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

polyval()用于计算多项式的元素值。

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

参数:

  • coeffs。它是一个列表张量,代表多项式的系数。
  • x:它是一个张量,代表多项式的变量。
  • name(可选):它定义了该操作的名称。

返回:它返回一个张量。

如果 coeffs 是一个有n个值的张量,x是一个张量,那么P(x)是n阶多项式,定义为:。

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

示例 1:

# 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:

# 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)