【发布时间】:2020-03-15 19:21:15
【问题描述】:
我遇到了生成器Python 的问题。在创建生成器以使用 Keras 进行训练时,我希望每次都有不同的值。但是,即使使用锁,我也只能在epoch 期间获得相同的值。 Keras 是否使用生成器差异?
def image_generator_for_filter_language_chinese(self):
while True:
with self.counter_lock:
*local_count = self.globals_count
self.globals_count += 1*
print("count_batch", local_count, ", ", self.numb_sample)
local_count %= self.batch_per_epoch
with self.lmdb_keys_lock:
batch_list = self.lmdb_keys[int(local_count * self.batch_size):
int(min((local_count + 1) * self.batch_size, self.numb_samples_data))]
batch_lmdb_keys = np.random.RandomState().choice(a=batch_list, size=self.batch_size)
batch_x, batch_y = self.get_data_from_lmdb(batch_lmdb_keys, True)
if local_count == 0:
with self.lmdb_keys_lock:
random.shuffle(self.lmdb_keys)
yield (batch_x, batch_y)
使用keras
model.fit_generator(
train_gen.image_generator_for_filter_language_chinese(),
verbose=1,
steps_per_epoch=40000/batch_size,
epochs=nb_epochs,
use_multiprocessing=True,
validation_data=val_gen.image_generator_for_filter_language_chinese(),
validation_steps=10000/128,
workers=num_thread,
callbacks=cbs)
我得到结果
count_batch 14 , 250
count_batch 13 , 250
count_batch 13 , 250
count_batch 14 , 250
count_batch 14 , 250
count_batch 14 , 250
count_batch 14 , 250
count_batch 14 , 250
count_batch 14 , 250
count_batch 14 , 250
count_batch 15 , 250
count_batch 14 , 250
count_batch 14 , 250
count_batch 15 , 250
count_batch 15 , 250
count_batch 15 , 250
count_batch 15 , 250
count_batch 15 , 250
count_batch 15 , 250
count_batch 15 , 250
count_batch 16 , 250
count_batch 15 , 250
count_batch 15 , 250
count_batch 16 , 250
count_batch 16 , 250
count_batch 16 , 250
count_batch 16 , 250
count_batch 16 , 250
count_batch 16 , 250
count_batch 16 , 250
count_batch 17 , 250
为什么count_batch 没有按我的意愿更改?
如果我使用单线程,这种方法非常有效,但多线程无法按我的预期工作。
提前谢谢你
【问题讨论】:
-
你的 batch_size 值是多少?
-
我的 batch_size 是 128。我认为问题与 batch_size 无关。唯一的问题是 count_batch 面临竞争条件,即使我使用了锁。
-
steps_per_epoch 必须是整数。所以你可能想做 ceil(40000/batch_size)
-
它不起作用。结果还是一样。