sklearn.utils.shuffle-训练数据打乱的最佳方法

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

 

posted @   今夜无风  阅读(1947)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示