How to shuffle a Tensor using Pytorch C++ API

Hello,

Is there a way to shuffle the data in a tensor when using Pytorch C++ API without resorting to a loop to access each element randomly?

Thanks!

This is what I am using, directly from my code, just disregard mesh, cc, and vf and use your own input data

// - Randomly shuffle cell center indices. 
    torch::Tensor shuffled_indices = torch::randperm(
        mesh.nCells(),
        torch::TensorOptions().dtype(at::kLong)
    ); 
    // - Randomly select 10 % of all cell centers for training. 
    long int n_cells = int(0.1 * mesh.nCells());
    torch::Tensor training_indices = shuffled_indices.index({Slice(0, n_cells)}); 
    
    // - Use 10% of random indices to select the training_data from vf_tensor 
    torch::Tensor vf_training = vf_tensor.index(training_indices);
    torch::Tensor cc_training = cc_tensor.index(training_indices);
1 Like