Keras Model.fit_generator中这三个参数的说明
max_queue_size
:
-
maximum size of the internal training queue which is used to "precache" samples from the generator
-
预缓存的最大队列数量
workers
:
-
number of threads generating batches in parallel. Batches are computed in parallel on the CPU and passed on the fly onto the GPU for neural network computations
-
获取batch时的“线程”并行数
use_multiprocessing
:
-
whether to use process-based threading
-
是否使用 基于进程的线程,猜测是选择进程并行还是线程并行
在fit_generator的demo设置中多采用max_queue_size=10, workers=1, use_multiprocessing=False
max_queue_size
keras训练时会持续创建batch直到缓存队列达到max_queue_size的设置或者训练结束时。使用缓存的好处是在GPU运算快时,避免GPU等待CPU生成batch而浪费时间。
workers
workers是CPU准备数据的并行数,和max_queue_size相似,如果GPU在等数据,调大workers和max_queue_size可以更快的准备数据,避免GPU空转。
use_multiprocessing
是否使用基于进程的线程,对于keras原生的数据生成器或者线程安全的数据生成器,使用True时一般不会出现问题,但是自定义的数据生成器在workers大于1时设置本参数True可能会出现阻塞的问题,例子,此时用False可以避免问题。如果使用多个生成器时,只有设置True时才能有效加速。
参考:
Tip – fit_generator in keras – how to parallelise correctly – Keunwoo Choi (wordpress.com)