【问题标题】:Python generator doesnot working with KerasPython 生成器不适用于 Keras
【发布时间】: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)
  • 它不起作用。结果还是一样。

标签: python keras generator


【解决方案1】:

使用多处理机制可以解决问题。

from multiprocessing import Value

globals_count = Value(ctypes.c_int, int(0))
def __get_count(self):
        tmp = self.globals_count.value
        with self.globals_count.get_lock():
            self.globals_count.value += 1
        return tmp

现在完美运行。问题是 Keras 使用多处理,而我只使用多线程锁来处理。

【讨论】:

    【解决方案2】:
    import stdio
    import sys
    from markov_model import MarkovModel
    def main()
    

    提示:

    • 读取命令行参数 k 和 s
    • 使用 sys.stdin.read() 将文本初始化为从标准输入读取的文本
    • 使用文本和 k 创建马尔可夫模型模型
    • 使用 model.replace_unknown() 解码损坏的文本 s
    • 将解码后的文本写入标准输出
    • 使用下面幻灯片中建议的想法在 MarkModel 中实现方法 replace_unknown()

    【讨论】:

      猜你喜欢
      • 2019-04-04
      • 1970-01-01
      • 2017-09-08
      • 2017-03-29
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      相关资源
      最近更新 更多