The code looks alright.
You might pre-compute the indices and use scatter, which might be a bit faster:
idx = torch.stack([torch.randperm(n) for _ in range(28*28)]).t().long()
data_shuffled = torch.zeros(10, 784).scatter_(0, idx, data.view(10, -1)).view(10, 28, 28)