【问题标题】:How to define max_queue_size, workers and use_multiprocessing in keras fit_generator()?如何在 keras fit_generator() 中定义 max_queue_size、workers 和 use_multiprocessing?
【发布时间】:2019-08-27 02:58:06
【问题描述】:

我正在使用 GPU 版本的 keras 在预训练网络上应用迁移学习。我不明白如何定义参数max_queue_size、workers和use_multiprocessing。如果我更改这些参数(主要是为了加快学习速度),我不确定每个 epoch 是否仍然可以看到所有数据。

max_queue_size:

  • 用于从生成器“预缓存”样本的内部训练队列的最大大小

  • 问题:这是指在 CPU 上准备了多少批次?它与workers 有什么关系?如何优化定义?

workers:

  • 并行生成批次的线程数。批处理在 CPU 上并行计算,并即时传递到 GPU 上进行神经网络计算

  • 问题:我如何知道我的 CPU 可以/应该并行生成多少批次?

use_multiprocessing:

  • 是否使用基于进程的线程

  • 问题:如果我更改workers,是否必须将此参数设置为true?是否与 CPU 使用率有关?

相关问题可以在这里找到:

我使用fit_generator()如下:

    history = model.fit_generator(generator=trainGenerator,
                                  steps_per_epoch=trainGenerator.samples//nBatches,     # total number of steps (batches of samples)
                                  epochs=nEpochs,                   # number of epochs to train the model
                                  verbose=2,                        # verbosity mode. 0 = silent, 1 = progress bar, 2 = one line per epoch
                                  callbacks=callback,               # keras.callbacks.Callback instances to apply during training
                                  validation_data=valGenerator,     # generator or tuple on which to evaluate the loss and any model metrics at the end of each epoch
                                  validation_steps=
                                  valGenerator.samples//nBatches,   # number of steps (batches of samples) to yield from validation_data generator before stopping at the end of every epoch
                                  class_weight=classWeights,                # optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function
                                  max_queue_size=10,                # maximum size for the generator queue
                                  workers=1,                        # maximum number of processes to spin up when using process-based threading
                                  use_multiprocessing=False,        # whether to use process-based threading
                                  shuffle=True,                     # whether to shuffle the order of the batches at the beginning of each epoch
                                  initial_epoch=0)   

我的机器的规格是:

CPU : 2xXeon E5-2260 2.6 GHz
Cores: 10
Graphic card: Titan X, Maxwell, GM200
RAM: 128 GB
HDD: 4TB
SSD: 512 GB

【问题讨论】:

    标签: python tensorflow machine-learning keras gpu


    【解决方案1】:

    Q_0:

    问题:这是指在 CPU 上准备了多少个批次?它与工人有什么关系?如何优化定义?

    从您发布的link 中,您可以了解到您的 CPU 会不断创建批次,直到队列达到最大队列大小或达到停止状态。您希望为 GPU 准备好批次以“获取”,这样 GPU 就不必等待 CPU。 队列大小的理想值是使其足够大,以使您的 GPU 始终在最大值附近运行,并且永远不必等待 CPU 准备新批次。

    Q_1:

    问题:我如何知道我的 CPU 可以/应该并行生成多少批次?

    如果您发现您的 GPU 处于空闲状态并等待批处理,请尝试增加工作人员的数量,或许还可以增加队列大小。

    Q_2:

    如果我更换工人,我是否必须将此参数设置为 true?跟CPU使用有关系吗?

    Here 是对将其设置为True 或False 时发生的情况的实际分析。 Here 建议将其设置为 False 以防止冻结(在我的设置中 True 可以正常工作而不会冻结)。也许其他人可以增加我们对该主题的理解。

    总结:

    尽量不要有顺序设置,尽量让 CPU 为 GPU 提供足够的数据。

    另外:您可以(应该?)下次创建几个问题,以便更容易回答。

    【讨论】:

    • 非常有帮助,但我不同意问题发布者应该单独提出这些问题。这些问题是相关的,例如您在贡献的最后做了一句话总结。
    猜你喜欢
    • 1970-01-01
    • 2019-10-26
    • 2016-12-20
    • 2018-03-30
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 2020-04-25
    相关资源
    最近更新 更多