Search code examples
numpydeep-learningneural-networkpytorch

Is there a power function for tensors in pytorch?


Is there an equivalent of numpy.power in pytorch?

The function basically raises each element in the first tensor to the power represented in each corresponding element in the second tensor.


Solution

  • You are looking for torch.pow.

    As mentioned by @Szymon Maszke, you can also use ** operator:

    y = torch.pow(x, y)
    # the same as
    y = x ** y
    # and also the same as
    y = x.pow(y)
    

    where y can be a scalar or a tensor with shape broadcastable to x's shape.