如何使用Keras进行多GPU训练?

11

我希望我的模型可以在多个GPU上运行,共享参数但使用不同的数据批次。

我能否使用model.fit()实现这样的功能?还有其他替代方案吗?

3个回答

3

2

0
中,多GPU模型训练比以往任何时候都更加方便。请查看以下文档了解详情:多GPU和分布式训练

本质上,要使用模型进行单主机、多设备同步训练,您需要使用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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接