.fit() 层的 shuffle = 'batch' 参数如何在后台工作?

How does shuffle = 'batch' argument of the .fit() layer work in the background?

当我使用 .fit() 层训练模型时,参数 shuffle 预设为 True。

假设我的数据集有 100 个样本,批量大小为 10。当我设置 shuffle = True 时,keras 首先随机选择样本(现在 100 个样本有不同的顺序),然​​后在新订单将开始创建批次:批次 1:1-10,批次 2:11-20 等

如果我设置 shuffle = 'batch' 它应该如何在后台运行?直观地,使用前面的 100 个样本数据集的例子,批量大小 = 10 我的猜测是 keras 首先将样本分配给批次(即批次 1:样本 1-10 按照数据集原始顺序,批次 2:11-20 以下数据集的原始顺序,批次 3 ...等等),然后打乱批次的顺序。因此,模型现在将在随机排序的批次上进行训练,例如:3(包含样本 21 - 30)、4(包含样本 31 - 40)、7(包含样本 61 - 70)、1(包含样本 1 - 10 ), ... (我编的批次顺序).

我的想法是对的还是我漏掉了什么?

谢谢!

看看这个 link 的实现(training.py 的第 349 行),答案似乎是肯定的。

尝试使用此代码进行检查:

import numpy as np
def batch_shuffle(index_array, batch_size):
    """Shuffles an array in a batch-wise fashion.
    Useful for shuffling HDF5 arrays
    (where one cannot access arbitrary indices).
    # Arguments
        index_array: array of indices to be shuffled.
        batch_size: integer.
    # Returns
        The `index_array` array, shuffled in a batch-wise fashion.
    """
    batch_count = int(len(index_array) / batch_size)
    # to reshape we need to be cleanly divisible by batch size
    # we stash extra items and reappend them after shuffling
    last_batch = index_array[batch_count * batch_size:]
    index_array = index_array[:batch_count * batch_size]
    index_array = index_array.reshape((batch_count, batch_size))
    np.random.shuffle(index_array)
    index_array = index_array.flatten()
    return np.append(index_array, last_batch)


x = np.array(range(100))
x_s = batch_shuffle(x,10)