【问题标题】:How does shuffle = 'batch' argument of the .fit() layer work in the background?.fit() 层的 shuffle = 'batch' 参数如何在后台工作?
【发布时间】:2017-08-08 11:54:41
【问题描述】:

当我使用.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 ), ... (批次的顺序是我编的)。

我的想法是对的还是我错过了什么?

谢谢!

【问题讨论】:

    标签: keras shuffle lstm rnn


    【解决方案1】:

    查看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)
    

    【讨论】:

      猜你喜欢
      • 2020-01-15
      • 2011-07-07
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多