Tensorflow:np数组的next_batch函数

时间:2017-07-14 19:08:56

标签: python machine-learning tensorflow

我将数据训练为

xTrain = numpy.asarray([100, 1, 5, 6 ...])
yTrain = numpy.asarray([200, 2, 10, 12 ...])

如何定义next_batch(size)方法以从列车数据中获取随机元素的大小数量。

1 个答案:

答案 0 :(得分:1)

您可以将其用作next batch功能:

def batch_data(source, target, batch_size):

   # Shuffle data
   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)
相关问题