1.pycharm里直接在代码中加入下面
import os os.environ["CUDA_VISIBLE_DEVICES"] = "2"
2.在终端指定使用
CUDA_VISIBLE_DEVICES=2 python xxx.py
内容源自:https://www.cnblogs.com/darkknightzh/p/6836568.html