Keras fit()和fit_generator()给出不同的结果。我实现了这两种方法,使所有其他参数保持不变。我在下面附加了数据生成器和模型。该模型取自此站点。 https://machinelearningmastery.com/
在数据生成器中,我正在从硬盘驱动器加载数据。每个X_train文件都包含一个大小为(3,1)的矩阵。例如,如果批处理大小为2,则X_batch的大小将为(2,3,1)。
def generator(list_xtrain, list_ytrain, batch_size):
samples_per_epoch = len(list_xtrain)
number_of_batches = samples_per_epoch/batch_size
counter=0
X_batch = np.empty((batch_size,3,1))
y_batch = np.empty((batch_size))
while 1:
temp_listx = list_xtrain[batch_size*counter:batch_size*(counter+1)]
temp_listy = list_ytrain[batch_size*counter:batch_size*(counter+1)]
for i, ID in enumerate(temp_listx):
X_batch[i,] = np.load('F:/Air_passenger_data_gen/' + ID)
for j, ID in enumerate(temp_listy):
# Store class
y_batch[j] = np.load('F:/Air_passenger_data_gen/' + ID)
counter += 1
yield X_batch,y_batch
#restart counter to yeild data in the next epoch as well
if counter >= number_of_batches:
counter = 0
#using fit_generator()
batch_size=2
model.fit_generator(generator=generator(list_xtrain, list_ytrain,
batch_size),
epochs=100,
steps_per_epoch=len(list_xtrain)/batch_size,
verbose=2,
use_multiprocessing=False,
workers=4)
#using fit()
model.fit(trainX, trainY, epochs=100, batch_size=2)
我希望输出与fit()的输出相同。但是使用fit_generator()会带来一些疯狂的损失= 41781.00,而使用fit()时,它会损失0.0020