Last active
August 31, 2020 17:27
-
-
Save sheikirfanbasha/0eb2896ec86e5c3c92e16d2eaee762b4 to your computer and use it in GitHub Desktop.
Simple technique to do shuffling when the order can be random
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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