我想知道设置用于创建/训练模型的设备的正确方法是什么,以便通过Keras API在TensorFlow中优化资源使用以进行快速训练?我可以使用1个CPU和2个GPU。我最初使用tf.device
上下文创建模型并仅在GPU上进行训练,但是后来在tf.keras.utils.multi_gpu_model
的TensorFlow文档中看到,他们建议在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上训练模型仍然更好吗?