一、TensorFlow 指定計算使用的 GPU 卡
~$ CUDA_VISIBLE_DEVICES=0 python file_name.py
~$ CUDA_VISIBLE_DEVICES=1 python file_name.py
~$ CUDA_VISIBLE_DEVICES=0,1,2 python file_name.py
~$ CUDA_VISIBLE_DEVICES="0,1,2" python file_name.py
~$ CUDA_VISIBLE_DEVICES="" python file_name.py
>>> import os
>>> os.environ["CUDA_VISIBLE_DEVICES"] = "0"
>>> os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2"
二、TensorFlow 設置使用顯存的大小
- 方法一:設置 config 對象的屬性:gpu_options.per_process_gpu_memory_fraction
>>> config = tf.ConfigProto()
>>> config.gpu_options.per_process_gpu_mem>>> ory_fraction = 0.6
>>> session = tf.Session(config=config)
- 方法二:創建 GPUOptions 時設置參數:per_process_gpu_memory_fraction
>>> gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
>>> config=tf.ConfigProto(gpu_options=gpu_options)
>>> session = tf.Session(config=config)
三、按需動態使用 GPU 資源
>>> config = tf.ConfigProto()
>>> config.gpu_options.allow_growth = True
>>> session = tf.Session(config=config)
四、查看 GPU 使用情況
~$ nvidia-smi