评价此页

torch.pow#

torch.pow(input, exponent, *, out=None) Tensor#

input 中每个元素的 exponent 次幂,并返回一个包含结果的张量。

exponent 可以是一个单独的 float 数字,也可以是一个与 input 具有相同元素数量的 Tensor

exponent 是一个标量值时,应用的操作是:

outi=xiexponent\text{out}_i = x_i ^ \text{exponent}

exponent 是一个张量时,应用的操作是:

outi=xiexponenti\text{out}_i = x_i ^ {\text{exponent}_i}

exponent 是一个张量时,inputexponent 的形状必须 可广播

参数
  • input (Tensor) – 输入张量。

  • exponent (floattensor) – 指数值

关键字参数

out (Tensor, optional) – 输出张量。

示例

>>> a = torch.randn(4)
>>> a
tensor([ 0.4331,  1.2475,  0.6834, -0.2791])
>>> torch.pow(a, 2)
tensor([ 0.1875,  1.5561,  0.4670,  0.0779])
>>> exp = torch.arange(1., 5.)

>>> a = torch.arange(1., 5.)
>>> a
tensor([ 1.,  2.,  3.,  4.])
>>> exp
tensor([ 1.,  2.,  3.,  4.])
>>> torch.pow(a, exp)
tensor([   1.,    4.,   27.,  256.])
torch.pow(self, exponent, *, out=None) Tensor

self 是一个标量 float 值,exponent 是一个张量。返回的张量 out 的形状与 exponent 相同。

应用的操作是:

outi=selfexponenti\text{out}_i = \text{self} ^ {\text{exponent}_i}
参数
  • self (float) – 幂运算的标量基值

  • exponent (Tensor) – 指数张量

关键字参数

out (Tensor, optional) – 输出张量。

示例

>>> exp = torch.arange(1., 5.)
>>> base = 2
>>> torch.pow(base, exp)
tensor([  2.,   4.,   8.,  16.])