2017-08-08 87 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)