2017-08-08 88 views
3

当我使用.fit()图层训练模型时,参数shuffle预设为True。.fit()图层的shuffle ='batch'参数如何在后台工作?

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

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

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

谢谢!

回答

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)