Vin B.
Vin B.

Reputation: 41

Torch tensor with i-th element as product of all previous

I have a torch tensor like this one:

A = torch.tensor([1,2,3,4,5,6])

there's in torch a simple way to generate a tensor like this?

[1, 1*2, 1*2*3, 1*2*3*4, 1*2*3*4*5, 1*2*3*4*5*6]

Thanks a lot

Upvotes: 0

Views: 142

Answers (1)

Szymon Maszke
Szymon Maszke

Reputation: 24691

You can use torch.cumprod:

import torch

t = torch.tensor([1, 2, 3, 4, 5, 6])
torch.cumprod(t, dim=0)

which outputs:

tensor([1, 2, 6, 24, 120, 720])

Upvotes: 1

Related Questions