2017-06-29 14 views
1

タイトルによれば、私はkerasのfit_generatorメソッドを使用しようとしています。keras fit_generatorの使い方

私は50x50の画像で作業しています。いくつかの前処理した後、これは私が持っているものです。

print(X_train.shape) 
print(y_train.shape) 
print(X_test.shape) 
print(y_test.shape) 

(122, 50, 50, 1) 
(122, 15) 
(41, 50, 50, 1) 
(41, 15) 

これは(hereから来ている)ジェネレータです:

def generator(features, labels, batch_size): 
    # Create empty arrays to contain batch of features and labels# 
    batch_features = np.zeros((batch_size, size, size, 1)) 
    batch_labels = np.zeros((batch_size, n_targets)) 
    while True: 
     for i in range(batch_size): 
      # choose random index in features 
      index = random.choice(len(features),1) 
      batch_features[i] = features[index] 
      batch_labels[i] = labels[index] 
     yield batch_features, batch_labels 

そして、私が使用して呼び出さ:しかし

batch_size = 32 
start_time = time.time() 

model = create_model() 
hist = model.fit_generator(generator(X_train, y_train, batch_size=batch_size), 
          steps_per_epoch=X_train.shape[0] // batch_size, 
          epochs=50, verbose=0, validation_data=(X_test, y_test)) 
# hist = model.fit(X_train, y_train, batch_size=16, epochs=100, verbose=0, validation_data=(X_test, y_test)) 

print("--- %s seconds ---" % (time.time() - start_time)) 

これは私にエラーを与える:

ValueError: output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)`. Found: None 

答えて

0

私はあなたのコードを実行し、私はこの問題は、あなたがindex = np.random.choice(len(features),1)にラインindex = random.choice(len(features),1)を変更する必要があることであると信じています(注意追加np.

私はお互いの後に2つのエラーを取得するコードを実行すると、最初にあることを示しているのでランダムが見つかりません。秒は、タプルが生成されないことを示します。だから、おそらく最初のエラーを逃したかもしれませんか?

問題の行を変更すると、すべて正常に動作しているようです。

ところで、sizeおよびn_targetsもまた、もちろん、範囲内で定義する必要があります。

関連する問題