我的训练数据如下:
xTrain = numpy.asarray([100, 1, 5, 6 ...])yTrain = numpy.asarray([200, 2, 10, 12 ...])
如何定义next_batch(size)方法,从训练数据中随机获取指定数量的元素。
回答:
你可以使用以下代码作为你的next batch
函数:
def batch_data(source, target, batch_size): # 打乱数据顺序
shuffle_indices = np.random.permutation(np.arange(len(target)))
source = source[shuffle_indices]
target = target[shuffle_indices]
for batch_i in range(0, len(source)//batch_size):
start_i = batch_i * batch_size
source_batch = source[start_i:start_i + batch_size]
target_batch = target[start_i:start_i + batch_size]
yield np.array(source_batch), np.array(target_batch)