在进行模型训练前,我们要将数据打乱,以获得更好的训练效果。可以使用sklearn.utils中的shuffle,获得打乱后的数据索引,最后,迭代生成打乱后的batch数据,一个写好的模块如下。
思路是:1.先shuffle 2.再迭代生成
1 def fill_feed_dict(data_X, data_Y, batch_size): 2 """Generator to yield batches""" 3 # Shuffle data first. 4 shuffled_X, shuffled_Y = shuffle(data_X, data_Y) 5 # print("before shuffle: ", data_Y[:10]) 6 # print(data_X.shape[0]) 7 # perm = np.random.permutation(data_X.shape[0]) 8 # data_X = data_X[perm] 9 # shuffled_Y = data_Y[perm] 10 # print("after shuffle: ", shuffled_Y[:10]) 11 for idx in range(data_X.shape[0] // batch_size): 12 x_batch = shuffled_X[batch_size * idx: batch_size * (idx + 1)] 13 y_batch = shuffled_Y[batch_size * idx: batch_size * (idx + 1)] 14 yield x_batch, y_batch