1
正如標題所說,我試圖使用從keras的fit_generator
方法。如何使用keras fit_generator正確
我用50×50的圖像工作。一些預處理後,這是我有:
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