torch.pow¶
- torch.pow(input, exponent, *, out=None) Tensor ¶
計算
input
中每個元素的exponent
次方,並返回一個包含結果的張量。exponent
可以是一個單一的float
數字,或是一個與input
具有相同元素數量的 Tensor。當
exponent
是一個純量值時,應用的運算是當
exponent
是一個張量時,應用的運算是當
exponent
是一個張量時,input
和exponent
的形狀必須是 可廣播的。範例
>>> 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
的形狀相同應用的運算是
範例
>>> exp = torch.arange(1., 5.) >>> base = 2 >>> torch.pow(base, exp) tensor([ 2., 4., 8., 16.])