快捷鍵

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 是一個張量。 返回的張量 outexponent 的形狀相同

應用的運算是

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.])

文件

存取 PyTorch 的完整開發者文件

檢視文件

教學課程

取得為初學者和進階開發人員提供的深入教學課程

檢視教學課程

資源

尋找開發資源並獲得您問題的解答

檢視資源