深度学习中GPU的使用(python)

参考:https://www.jianshu.com/p/076998e3c3dd

记录设备指派情况

为了获取你的 operations 和 Tensor 被指派到哪个设备上运行, 用 log_device_placement 新建一个 session, 并设置为 True.

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

代码中指定GPU

  • 方法一
with tf.Session as sess:  
    with tf.device('/gpu:1'):  
        ...  

//若想在多块GPU上运行  
with tf.Session as sess:  
    for d in ['/gpu:1','/gpu:2']:  
        with tf.device(d):  
            ...  
  • 方法二
import os
os.environ["CUDA_DEVICES_ORDER"]="PCI_BUS_IS"
os.environ["CUDA_VISIBLE_DEVICES"]=1或"1"
//指定使用第二块GPU

终端中指定GPU

CUDA_VISIBLE_DEVICES=O,1 python3 main.py //程序只能使用GPU:1
CUDA_VISIBLE_DEVICES="0,1" //同上
CUDA_VISIBLE_DEVICES="" //禁止程序使用GPU

InvalidArgumentError GPU不能使用的问题

allow_soft_placement=True,就是在GPU跑不动程序的时候,自动切换到CPU运行,这种方式虽然能够确保程序的正常运行,但是却不能保证GPU出现问题能够及时的反馈.

查看GPU的状态

nvidia-smi //显示此刻状态
nvidia-smi -l //每隔几秒动态显示GPU状态(小写L)  
watch -n 10 nvidia-smi //每隔10秒动态显示GPU状态

猜你喜欢

转载自blog.csdn.net/fu6543210/article/details/82895315