Tensorflow全局设置可见GPU编号操作

 更新时间:2020年06月30日 09:29:33   作者:silent56_th  
这篇文章主要介绍了Tensorflow全局设置可见GPU编号操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

笔者需要tensorflow仅运行在一个GPU上(机器本身有多GPU),而且需要依据系统参数动态调节,故无法简单使用CUDA_VISIBLE_DEVICES。

一种方式是全局使用tf.device函数生成的域,但设备号需要在绘制Graph前指定,仍然不够灵活。

查阅文档发现config的GPUOptions中的visible_device_list可以定义GPU编号从visible到virtual的映射,即可以设置tensorflow可见的GPU device,从而全局设置了tensorflow可见的GPU编号。代码如下:

config = tf.ConfigProto()
config.gpu_options.visible_device_list = str(device_num)
sess = tf.Session(config=config)

参考 多卡服务器下隐藏部分 GPU 和 TensorFlow 的显存使用设置,还可以通过os包设置全局变量CUDA_VISIBLE_DEVICES,代码如下:

os.environ["CUDA_VISIBLE_DEVICES"] = "2"

补充知识:TensorFlow 设置程序可见GPU与逻辑分区

TensorFlow 设置程序可见GPU(多GPU情况)

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf

from tensorflow_core.python.keras.api._v2 import keras

print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
 print(module.__name__, module.__version__)

# 打印变量所在位置
tf.debugging.set_log_device_placement(True) 

# 获取物理GPU的个数
gpus = tf.config.experimental.list_physical_devices("GPU") 

if len(gpus) >= 1:
 # 设置第几个GPU 当前程序可见
 tf.config.experimental.set_visible_devices(gpus[0], "GPU")
 
print("物理GPU个数:", len(gpus))

# 获取逻辑GPU的个数
logical_gpus = tf.config.experimental.list_logical_devices("GPU") 
print("逻辑GPU个数:", len(logical_gpus))

TensorFlow 设置GPU的 逻辑分区

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf

from tensorflow_core.python.keras.api._v2 import keras

print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
 print(module.__name__, module.__version__)

# 打印变量所在位置
tf.debugging.set_log_device_placement(True) 

# 获取物理GPU的个数
gpus = tf.config.experimental.list_physical_devices("GPU") 

if len(gpus) >= 1:
 # 设置第几个GPU 当前程序可见
 tf.config.experimental.set_visible_devices(gpus[0], "GPU")
 
 # 设置GPU的 逻辑分区
 tf.config.experimental.set_virtual_device_configuration(
  gpus[0], 
  [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=3072),
   tf.config.experimental.VirtualDeviceConfiguration(memory_limit=3072)])

print("物理GPU个数:", len(gpus))

# 获取逻辑GPU的个数
logical_gpus = tf.config.experimental.list_logical_devices("GPU") 
print("逻辑GPU个数:", len(logical_gpus))

TensorFlow 手动设置处理GPU

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf

from tensorflow_core.python.keras.api._v2 import keras

print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
 print(module.__name__, module.__version__)

# 打印变量所在位置
tf.debugging.set_log_device_placement(True) 

# 自动指定处理设备
tf.config.set_soft_device_placement(True)

# 获取物理GPU的个数
gpus = tf.config.experimental.list_physical_devices("GPU") 
for gpu in gpus:
 # 设置内存自增长方式
 tf.config.experimental.set_memory_growth(gpu, True) 
print("物理GPU个数:", len(gpus))

# 获取逻辑GPU的个数
logical_gpus = tf.config.experimental.list_logical_devices("GPU") 
print("逻辑GPU个数:", len(logical_gpus))

c = []

# 循环遍历当前逻辑GPU
for gpu in logical_gpus:
 print(gpu.name)

 # 手动设置处理GPU
 with tf.device(gpu.name):
  a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
  b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
  
  # 矩阵相乘 并且添加至列表
  c.append(tf.matmul(a, b))

# 手动设置处理GPU
with tf.device("/GPU:0"):
 matmul_sum = tf.add_n(c)

print(matmul_sum)

以上这篇Tensorflow全局设置可见GPU编号操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python 使用 pyc 解决明文密钥问题记录

    Python 使用 pyc 解决明文密钥问题记录

    pyc 是 Python 经过 compile 后的文件类型,一段 Python 代码执行前会先将 .py 文件编译成 .pyc 文件它是一种字节码 byte code,然后由 Python 虚拟机执行,这篇文章主要介绍了Python使用pyc解决明文密钥问题,需要的朋友可以参考下
    2023-07-07
  • Python如何给函数库增加日志功能

    Python如何给函数库增加日志功能

    这篇文章主要介绍了Python如何给函数库增加日志功能,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-08-08
  • 用Python实现简单的人脸识别功能步骤详解

    用Python实现简单的人脸识别功能步骤详解

    这篇文章主要介绍了用Python实现简单的人脸识别功能步骤详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • 如何通过pycharm实现对数据库的查询等操作(非多步操作)

    如何通过pycharm实现对数据库的查询等操作(非多步操作)

    这篇文章主要介绍了如何通过pycharm实现对数据库的查询等操作(非多步操作),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • Python 复平面绘图实例

    Python 复平面绘图实例

    今天小编就为大家分享一篇Python 复平面绘图实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • linux系统使用python监测网络接口获取网络的输入输出

    linux系统使用python监测网络接口获取网络的输入输出

    这篇文章主要介绍了linux系统使用python监测网络接口获取网络的输入输出信息,大家参考使用吧
    2014-01-01
  • Python操作Jira库常用方法解析

    Python操作Jira库常用方法解析

    这篇文章主要介绍了Python操作Jira库常用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Python模板的使用详细讲解

    Python模板的使用详细讲解

    Django 模板是使用 Django 模板语言标记的一个文本文档或Python字符串。模板引擎可以识别和解释一些构造。主要是变量和标签。模板是通过上下文来渲染的。渲染用变量的值替换变量,变量的值在上下文中查找,并执行标签。其他的一切都按原样输出
    2022-10-10
  • Python写一个字符串数字后缀部分的递增函数

    Python写一个字符串数字后缀部分的递增函数

    这篇文章主要介绍了Python写一个字符串数字后缀部分的递增函数,写函数之前需要Python处理重名字符串,添加或递增数字字符串后缀,下面具体过程,需要的小伙伴可以参考一下
    2022-03-03
  • Python中Json使用示例详解

    Python中Json使用示例详解

    这篇文章主要介绍了Python中Json使用,主要介绍一下python 中 json的使用 如何把dict转成json 、object 转成json 、以及json转成对象,需要的朋友可以参考下
    2022-07-07

最新评论