Something wrong happens when shuffle a tensor
I am trying to shuffle a tensor, but I find there is something wrong when tryting to using random.shuffle(),here is my code:

import numpy as np
import torch
import random

if __name__ == '__main__':

    label_0, label_1 = np.zeros([1, 1000])[0], np.ones([1, 1000])[0]
    test_label = np.hstack((label_0, label_1))

    num_0, num_1 = 0, 0

    for i in range(len(test_label)):
        if test_label[i] == 0:
            num_0 += 1
        elif test_label[i] == 1:
            num_1 += 1

    print('Num1:  ', num_1)
    print('Num0:  ', num_0)

    random.shuffle(test_label)

    num_0, num_1 = 0, 0
    for i in range(len(test_label)):
        if test_label[i] == 0:
            num_0 += 1
        elif test_label[i] == 1:
            num_1 += 1

    print('After Shuffle Num1:  ', num_1)
    print('After Shuffle Num0:  ', num_0)


If test_label is not a tensor, it works perfectly:

Num1:   1000
Num0:   1000
After Shuffle Num1:   1000
After Shuffle Num0:   1000


However when I try to transform the test_label from ndarray to tensor, something wrong would happend:

import numpy as np
import torch
import random

if __name__ == '__main__':

    label_0, label_1 = np.zeros([1, 1000])[0], np.ones([1, 1000])[0]
    test_label = np.hstack((label_0, label_1))

    # now transform to tensor
    test_label = torch.tensor(test_label)


    num_0, num_1 = 0, 0

    for i in range(len(test_label)):
        if test_label[i] == 0:
            num_0 += 1
        elif test_label[i] == 1:
            num_1 += 1

    print('Num1:  ', num_1)
    print('Num0:  ', num_0)

    random.shuffle(test_label)

    num_0, num_1 = 0, 0
    for i in range(len(test_label)):
        if test_label[i] == 0:
            num_0 += 1
        elif test_label[i] == 1:
            num_1 += 1

    print('After Shuffle Num1:  ', num_1)
    print('After Shuffle Num0:  ', num_0)


And the result seems to be kind wrong:

Num1:   1000
Num0:   1000
After Shuffle Num1:   306
After Shuffle Num0:   1694


Anybody can tell me why will make such problem?

You can use torch.randperm:

import numpy as np
import torch
import random

if __name__ == '__main__':

    label_0, label_1 = np.zeros([1, 1000])[0], np.ones([1, 1000])[0]
    test_label = np.hstack((label_0, label_1))

    # now transform to tensor
    test_label = torch.tensor(test_label)


    num_0, num_1 = 0, 0

    for i in range(len(test_label)):
        if test_label[i] == 0:
            num_0 += 1
        elif test_label[i] == 1:
            num_1 += 1

    print('Num1:  ', num_1)
    print('Num0:  ', num_0)

    p = torch.randperm(test_label.shape[0])
    test_label = test_label[p]

    num_0, num_1 = 0, 0
    for i in range(len(test_label)):
        if test_label[i] == 0:
            num_0 += 1
        elif test_label[i] == 1:
            num_1 += 1

    print('After Shuffle Num1:  ', num_1)
    print('After Shuffle Num0:  ', num_0)

