Russ Brown
Russ Brown

Reputation: 143

Randomly shuffling torch tensor

I have a tensor of batch size of 64. Each sample in the batch is of shape [4, 300]. So, shape of my batch is [64, 4, 300]. I want to randomly shuffle the elements of the batch. In other words, I want to shuffle all 64 [4, 300] tensors. How can I do this? The resulting tensor will obviously be of shape [64, 4, 300], but all the 64 rows of shape [4, 300], will be ordered differently.

Upvotes: 7

Views: 6055

Answers (1)

vlad ustimov
vlad ustimov

Reputation: 121

indexes = torch.randperm(data.shape[0])
data = data[indexes]

Upvotes: 9

Related Questions