在进行模型训练前,我们要将数据打乱,以获得更好的训练效果。可以使用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

 

相关文章:

  • 2021-11-02
  • 2022-12-23
  • 2021-06-15
  • 2022-12-23
  • 2022-01-14
  • 2021-09-24
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-30
  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2022-01-07
  • 2021-12-24
相关资源
相似解决方案