I have a tensor
x = [4,5,6]
I want to transform this tensor x
into the following tensor:
[[ 4, 5, 6],
[4**2, 5**2, 6**2],
[4**3, 5**3, 6**3]]
where each row is a higher degree polynomial than the previous.
I tried searching Pytorch documentation and found the repeat
function. This allows me to generate multiple rows of the same values, which I can then use a for-loop or map to modify the values.
I understand that using a for loop can solve this, but I am wondering if there is already an inbuilt function in Pytorch that lets me do the same.
The power function torch.pow
also accepts tensors as the exponent argument. Start by expanding your input tensor x
, column-wise:
>>> x = x[:,None].expand(-1,3)
tensor([[4, 4, 4],
[5, 5, 5],
[6, 6, 6]])
Then define your exponents as a range tensor:
>>> a = torch.arange(1,4)
tensor([1, 2, 3])
And compute your power tensor:
>>> x.pow(a).T
tensor([[ 4, 5, 6],
[ 16, 25, 36],
[ 64, 125, 216]])