shuffle generator

import random
import numpy as np

mode = 0

x = np.arange(100)### 假设这个为features
y = np.arange(100) ########## 假设这个为labels


def shuffle_aligned_list(data):
"""
Shuffle arrays in a list by shuffling each array identically.
"""
num = data[0].shape[0]
p = np.random.permutation(num)
return [d[p] for d in data]


def batch_generator(data, batch_size, shuffle=True):
"""
Generate batches of data.

Given a list of array-like objects, generate batches of a given
size by yielding a list of array-like objects corresponding to the
same slice of each input.
"""
if shuffle:
data = shuffle_aligned_list(data)

batch_count = 0
while True:
if batch_count * batch_size + batch_size > len(data[0]):
batch_count = 0

if shuffle:
data = shuffle_aligned_list(data)

start = batch_count * batch_size
end = start + batch_size
batch_count += 1
yield [d[start:end] for d in data]


# batch_data = batch_generator([x,y],batch_size=20,shuffle=True)

# print(next(batch_data))
# # epochs = 100
# next(batch_data)

[d[10:20] for d in shuffle_aligned_list([x,y])]

 

posted on 2020-12-01 15:06  nnnnnnnnnnnnnnnn  阅读(75)  评论(0编辑  收藏  举报

导航