使用一組特定的 GPU 裝置

要使用特定的 GPU 裝置集,可以使用 CUDA_VISIBLE_DEVICES 環境變數:

import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"]="0" # Will use only the first GPU device

os.environ["CUDA_VISIBLE_DEVICES"]="0,3" # Will use only the first and the fourth GPU devices

(引自這個答案 ; 這裡有關於 CUDA 環境變數的更多資訊。)