2017-08-08 2 views
3

.fit()レイヤーを使用してモデルをトレーニングすると、シャッフルがTrueにプリセットされています。.fit()レイヤのshuffle = 'batch'引数は、バックグラウンドでどのように動作しますか?

私のデータセットが100サンプルあり、バッチサイズが10であるとします。shuffle = Trueを設定すると、最初にkerasが無作為にサンプルをランダムに選択します(今度は100個のサンプルが異なる順番になります)。バッチの作成:バッチ1:1-10、バッチ2:11-20など

もし私がshuffle = 'batch'をバックグラウンドでどのように働かせるのですか?直観的に、バッチサイズ= 10の100サンプルデータセットの前の例を使用すると、ケラスは最初にバッチにサンプルを割り当てます(つまり、バッチ1:データセットの元の次のサンプル1〜10、バッチ2:データセットの元の順序も同様に、バッチ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) 
関連する問題