Skip to content

Instantly share code, notes, and snippets.

@sheikirfanbasha
Last active August 31, 2020 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheikirfanbasha/0eb2896ec86e5c3c92e16d2eaee762b4 to your computer and use it in GitHub Desktop.
Save sheikirfanbasha/0eb2896ec86e5c3c92e16d2eaee762b4 to your computer and use it in GitHub Desktop.
Simple technique to do shuffling when the order can be random
# Simple technique to do shuffling when the order can be random
import random
import torch
# generate 1-D array with 10 elements. With numbers ranging from 0 to 20
x = torch.randn(10) * 20
x.round_() # in-place rounding. Note the "_" suffix for "round" function.
indexes = list(range(len(x))) # Get the array indices
print(indexes); # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# set some random seed to reproduce the results correctly.
#Feel free to comment this line. Run the code and observe the output
random.seed(10);
random.shuffle(indexes); # in-place data shuffling
print(indexes); # [5, 2, 7, 1, 8, 4, 3, 6, 0, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment