Repeat with creating new last dimensions in a nice way

How do I repeat along last dimensions?

e.g. from a torch.rand(4) I would like to obtain a tensor of shape [4, 100, 5]

E.g. torch.rand(4).repeat(100, 5, 1).movedim(-1, 0).contiguous() or torch.rand(4)[..., None, None].expand(4, 100, 5).contiguous() works, but I wonder if there is a more colloquial version?

torch.randn(4, 1, 1).repeat((1, 100, 5))
I’d use the expand and contiguous version if only because repeat used to not be very efficient, so torch.randn(4, 1, 1).expand(-1, 100, 5).contiguous() if you need the contiguous.

Best regards

Thomas

1 Like

One problem is that if [100, 5] can be an arbtirary shape, we need to unsqueeze a dynamic number of times that hurts…

You mean torch.randn(4, *[1 for _ in s]).expand(-1, *s) ?

1 Like

Yeah, this kind. Thanks for this snippet.