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

1 Answer

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like