我希望我的模型可以在多个GPU上运行,共享参数但使用不同的数据批次。
我能否使用model.fit()
实现这样的功能?还有其他替代方案吗?
我希望我的模型可以在多个GPU上运行,共享参数但使用不同的数据批次。
我能否使用model.fit()
实现这样的功能?还有其他替代方案吗?
Keras现在已经内置了(从v2.0.9开始)跨多个GPU进行设备并行的支持,使用keras.utils.multi_gpu_model
。
目前只支持Tensorflow后端。
这里有一个很好的示例(文档):https://keras.io/getting-started/faq/#how-can-i-run-a-keras-model-on-multiple-gpus 也可以参考:https://datascience.stackexchange.com/a/25737
本质上,要使用keras模型进行单主机、多设备同步训练,您需要使用tf.distribute.MirroredStrategy
API。以下是它的工作原理:
实例化一个MirroredStrategy
,可选择配置要使用哪些特定设备(默认情况下,该策略将使用所有可用的GPU)。
使用策略对象打开一个作用域,在此作用域内创建包含变量的所有所需Keras对象。通常,这意味着在分发范围内创建和编译模型。
像往常一样通过fit()
训练模型。
示意图如下:
# Create a MirroredStrategy.
strategy = tf.distribute.MirroredStrategy()
print('Number of devices: {}'.format(strategy.num_replicas_in_sync))
# Open a strategy scope.
with strategy.scope():
# Everything that creates variables should be under the strategy scope.
# In general this is only model construction & `compile()`.
model = Model(...)
model.compile(...)
# Train the model on all available devices.
model.fit(train_dataset, validation_data=val_dataset, ...)
# Test the model on all available devices.
model.evaluate(test_dataset)