Tensorflow 指定GPU运行

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/philosophyatmath/article/details/81631840

If you have more than one GPU in your system, the GPU with the lowest ID will be selected by default. If you would like to run on a different GPU, you will need to specify the preference explicitly:(https://www.tensorflow.org/guide/using_gpu
如果您的系统中有多个 GPU,则默认情况下将选择 ID 最小的 GPU。如果您希望在其他 GPU 上运行,则需要显式指定偏好设置:

Environment Variable Syntax      Results
CUDA_VISIBLE_DEVICES=1           Only device 1 will be seen
CUDA_VISIBLE_DEVICES=0,1         Devices 0 and 1 will be visible
CUDA_VISIBLE_DEVICES="0,1"       Same as above, quotation marks are optional
CUDA_VISIBLE_DEVICES=0,2,3       Devices 0, 2, 3 will be visible; device 1 is masked
CUDA_VISIBLE_DEVICES=""          No GPU will be visible

nvidia-smi:查看gpu使用情况。

终端执行程序时设置使用的GPU

CUDA_VISIBLE_DEVICES=1 python my_script.py

python代码中设置使用的GPU

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "2,3"

设置tensorflow使用的显存大小

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))     

记录设备分配方式

要找出您的指令和张量被分配到哪个设备,请创建会话并将 log_device_placement 配置选项设为 True。

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

猜你喜欢

转载自blog.csdn.net/philosophyatmath/article/details/81631840