【发布时间】:2020-03-23 06:32:49
【问题描述】:
我想知道设置设备以创建/训练模型以优化资源使用以使用 Keras API 在 TensorFlow 中进行快速训练的正确方法是什么?我有 1 个 CPU 和 2 个 GPU 可供使用。我最初使用 tf.device 上下文来创建我的模型并仅在 GPU 上训练,但后来我在 TensorFlow 文档中看到 tf.keras.utils.multi_gpu_model,他们建议在 CPU 上显式实例化模型:
# Instantiate the base model (or "template" model).
# We recommend doing this with under a CPU device scope,
# so that the model's weights are hosted on CPU memory.
# Otherwise they may end up hosted on a GPU, which would
# complicate weight sharing.
with tf.device('/cpu:0'):
model = Xception(weights=None,
input_shape=(height, width, 3),
classes=num_classes)
# Replicates the model on 8 GPUs.
# This assumes that your machine has 8 available GPUs.
parallel_model = multi_gpu_model(model, gpus=8)
parallel_model.compile(loss='categorical_crossentropy',
optimizer='rmsprop')
我这样做了,现在当我训练时,我发现我的 CPU 使用率大幅上升,所有 8 个内核的使用率都达到了大约 70%,并且我的 GPU 内存已达到最大值。如果模型是在其中一个 GPU 上创建的,事情会变得更快吗?即使我只有 1 个 GPU,在 CPU 上创建模型并使用 tf.device 上下文在 GPU 上训练模型是否更好?
【问题讨论】:
标签: python tensorflow gpu cpu tf.keras