random.shuffle会对np.array()产生很奇怪的反应,常常会出现问题,比如在tensorlow中传递dict_feed 时,如果用random.shuffle打乱而不是用np.random.shuffle打乱,会对loss函数的值产生无法预测的影响
import random
import numpy as np
a = []
for i in range(10):
a.append([i]*5)
print('a:',a)
b = a[:]
b = np.array(b)
print('b:',b)
random.shuffle(a)
np.random.shuffle(b)
print('shuffle a:',a)
print('shuffle b:',b)
输出结果:
a: [[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [4, 4, 4, 4, 4], [5, 5, 5, 5, 5], [6, 6, 6, 6, 6], [7, 7, 7, 7, 7], [8, 8, 8, 8, 8], [9, 9, 9, 9, 9]]
b: [[0 0 0 0 0]
[1 1 1 1 1]
[2 2 2 2 2]
[3 3 3 3 3]
[4 4 4 4 4]
[5 5 5 5 5]
[6 6 6 6 6]
[7 7 7 7 7]
[8 8 8 8 8]
[9 9 9 9 9]]
shuffle a: [[6, 6, 6, 6, 6], [8, 8, 8, 8, 8], [0, 0, 0, 0, 0], [2, 2, 2, 2, 2], [4, 4, 4, 4, 4], [7, 7, 7, 7, 7], [1, 1, 1, 1, 1], [9, 9, 9, 9, 9], [3, 3, 3, 3, 3], [5, 5, 5, 5, 5]]
shuffle b: [[2 2 2 2 2]
[4 4 4 4 4]
[6 6 6 6 6]
[1 1 1 1 1]
[0 0 0 0 0]
[7 7 7 7 7]
[9 9 9 9 9]
[5 5 5 5 5]
[3 3 3 3 3]
[8 8 8 8 8]]