megengine.functional.pow

pow(x, y)[源代码]

Element-wise power.

一种特定实现的近似取幂运算,将张量 \(x\) 中的每一个元素 \(x_i\) (底数) 跟张量 \(y\) 中每一个元素 \(y_i\) (指数)做幂运算。

参数
  • x (Tensor) – 第一个输入 tensor 里的元素作为幂运算的底数。要求是数字数据类型。

  • y (Tensor) – 第二个输入 tensor 里的元素作为幂运算的指数。维度必须兼容 x (参见 广播机制与规则 )。要求是数字数据类型。

返回类型

Tensor

返回

返回一个逐元素运算之后的 Tensor,其数值类型由 类型提升规则 规则决定。

注解

可以使用 ** 运算符作为 pow 的简写来进行张量计算。

特殊的案例

对于浮点数的操作,

  • 如果 \(x_i\) 不为 1\(y_i\)NaN, 则幂运算的结果为 NaN.

  • If \(y_i\) is +0, the result is 1, even if \(x_i\) is NaN.

  • If \(y_i\) is -0, the result is 1, even if \(x_i\) is NaN.

  • If \(x_i\) is NaN and \(y_i\) is not equal to 0, the result is NaN.

  • If \(\abs{x_i}\) is greater than 1 and \(y_i\) is +infinity, the result is +infinity.

  • If \(\abs{x_i}\) is greater than 1 and \(y_i\) is -infinity, the result is +0.

  • If \(\abs{x_i}\) is 1 and \(y_i\) is +infinity, the result is 1.

  • If \(\abs{x_i}\) is 1 and \(y_i\) is -infinity, the result is 1.

  • If \(x_i\) is 1 and \(y_i\) is not NaN, the result is 1.

  • If \(\abs{x_i}\) is less than 1 and \(y_i\) is +infinity, the result is +0.

  • If \(\abs{x_i}\) is less than 1 and \(y_i\) is -infinity, the result is +infinity.

  • If \(x_i\) is +infinity and \(y_i\) is greater than 0, the result is +infinity.

  • If \(x_i\) is +infinity and \(y_i\) is less than 0, the result is +0.

  • If \(x_i\) is -infinity, \(y_i\) is greater than 0, and \(y_i\) is an odd integer value, the result is -infinity.

  • If \(x_i\) is -infinity, \(y_i\) is greater than 0, and \(y_i\) is not an odd integer value, the result is +infinity.

  • If \(x_i\) is -infinity, \(y_i\) is less than 0, and \(y_i\) is an odd integer value, the result is -0.

  • If \(x_i\) is -infinity, \(y_i\) is less than 0, and \(y_i\) is not an odd integer value, the result is +0.

  • If \(x_i\) is +0 and \(y_i\) is greater than 0, the result is +0.

  • If \(x_i\) is +0 and \(y_i\) is less than 0, the result is +infinity.

  • If \(x_i\) is -0, \(y_i\) is greater than 0, and \(y_i\) is an odd integer value, the result is -0.

  • If \(x_i\) is -0, \(y_i\) is greater than 0, and \(y_i\) is not an odd integer value, the result is +0.

  • If \(x_i\) is -0, \(y_i\) is less than 0, and \(y_i\) is an odd integer value, the result is -infinity.

  • If \(x_i\) is -0, \(y_i\) is less than 0, and \(y_i\) is not an odd integer value, the result is +infinity.

  • If \(x_i\) is less than 0, \(x_i\) is a finite number, \(y_i\) is a finite number, and \(y_i\) is not an integer value, the result is NaN.

实际案例

>>> F.pow(2.0, 3.0)
Tensor(8.0, device=xpux:0)

Element-wise power:

>>> x = Tensor([1, 2, 3, 4, 5])
>>> y = Tensor([1, 2, 1, 2, 1])
>>> F.pow(x, y)
Tensor([ 1.  4.  3. 16.  5.], device=xpux:0)

Broadcasting:

>>> F.pow(x, 2)
Tensor([ 1.  4.  9. 16. 25.], device=xpux:0)