Keras:针对多个大型数据集的批量培训

ten*_*con 11 deep-learning keras

这个问题考虑了Keras中多个大文件训练的常见问题,这些文件联合太大而无法适应GPU内存.我正在使用Keras 1.0.5,我想要一个不需要1.0.6的解决方案.fchollet 在这里这里描述了一种方法 :

# Create generator that yields (current features X, current labels y)
def BatchGenerator(files):
    for file in files:
        current_data = pickle.load(open("file", "rb"))
        X_train = current_data[:,:-1]
        y_train = current_data[:,-1]
        yield (X_train, y_train)

# train model on each dataset
for epoch in range(n_epochs):
    for (X_train, y_train) in BatchGenerator(files):
        model.fit(X_train, y_train, batch_size = 32, nb_epoch = 1)
Run Code Online (Sandbox Code Playgroud)

但是我担心模型的状态不会被保存,而是模型不仅在时期之间而且在数据集之间重新初始化.每个"Epoch 1/1"代表下面不同数据集的培训:

~~~~~大纪元0 ~~~~~~

大纪元1/1 295806/295806 [==============================] - 13s - 损失:15.7517
大纪元1/1 407890/407890 [==============================] - 19s - 损失:15.8036
大纪元1/1 383188/383188 [== ============================] - 19 - - 损失:15.8130
~~~~~大纪元1 ~~~~~~

大纪元1/1 295806/295806 [==============================] - 14s - 损失:15.7517
大纪元1/1 407890/407890 [==============================] - 20多岁 - 损失:15.8036
大纪元1/1 383188/383188 [== ============================] - 15s - 损失:15.8130

我知道可以使用model.fit_generator,但由于上面的方法被反复建议作为批量训练的一种方式,我想知道我做错了什么.

谢谢你的帮助,

马克斯

ten*_*con 8

我已经有一段时间没有遇到这个问题了,但我记得我使用
Kera 的功能通过 Python 生成器提供数据,即model = Sequential(); model.fit_generator(...).

示例代码片段(应该是不言自明的)

def generate_batches(files, batch_size):
   counter = 0
   while True:
     fname = files[counter]
     print(fname)
     counter = (counter + 1) % len(files)
     data_bundle = pickle.load(open(fname, "rb"))
     X_train = data_bundle[0].astype(np.float32)
     y_train = data_bundle[1].astype(np.float32)
     y_train = y_train.flatten()
     for cbatch in range(0, X_train.shape[0], batch_size):
         yield (X_train[cbatch:(cbatch + batch_size),:,:], y_train[cbatch:(cbatch + batch_size)])

model = Sequential()
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

train_files = [train_bundle_loc + "bundle_" + cb.__str__() for cb in range(nb_train_bundles)]
gen = generate_batches(files=train_files, batch_size=batch_size)
history = model.fit_generator(gen, samples_per_epoch=samples_per_epoch, nb_epoch=num_epoch,verbose=1, class_weight=class_weights)
Run Code Online (Sandbox Code Playgroud)