Tensorflow全局设置可见GPU编号操作
作者:silent56_th 时间:2021-04-21 12:41:46
笔者需要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)
来源:https://blog.csdn.net/silent56_th/article/details/81319597
标签:Tensorflow,全局,设置,GPU
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Oracle 游标使用总结
2009-10-02 17:36:00
Python图像处理库PIL的ImageDraw模块介绍详解
2022-06-29 04:18:20
![](https://img.aspxhome.com/file/2023/2/107652_0s.jpg)
javascript验证IP地址等验证例子
2007-09-11 13:40:00
PyTorch的SoftMax交叉熵损失和梯度用法
2023-06-17 12:46:49
设计评论表单[译]
2009-03-25 20:41:00
![](https://img.aspxhome.com/file/UploadPic/20093/25/contact_form-95s.png)
python使用htmllib分析网页内容的方法
2022-05-22 13:28:33
对json字符串与python字符串的不同之处详解
2023-11-22 04:35:39
python从PDF中提取数据的示例
2021-10-15 21:46:05
![](https://img.aspxhome.com/file/2023/2/99462_0s.png)
关于PyQt5主窗口图标显示问题汇总
2022-03-27 08:23:18
![](https://img.aspxhome.com/file/2023/9/85029_0s.png)
Python Tkinter Menu组件详解
2021-07-24 06:51:18
![](https://img.aspxhome.com/file/2023/3/81863_0s.png)
使用Python绘制图表大全总结
2022-08-09 20:35:27
![](https://img.aspxhome.com/file/2023/7/99517_0s.png)
Python用selenium实现自动登录和下单的项目实战
2021-02-04 18:29:44
![](https://img.aspxhome.com/file/2023/9/110199_0s.png)
一次MySQL性能优化实战
2009-03-09 15:01:00
对Python3 pyc 文件的使用详解
2023-05-02 01:54:57
python练习之循环控制语句 break 与 continue
2022-04-15 12:31:20
![](https://img.aspxhome.com/file/2023/1/86331_0s.png)
Python判断变量名是否合法的方法示例
2022-07-31 19:05:12
![](https://img.aspxhome.com/file/2023/1/68411_0s.png)
Python如何基于smtplib发不同格式的邮件
2023-10-03 10:28:35
python目标检测yolo1 yolo2 yolo3和SSD网络结构对比
2022-05-24 16:16:39
![](https://img.aspxhome.com/file/2023/6/92156_0s.jpg)
Django restful framework生成API文档过程详解
2021-08-10 16:37:16
![](https://img.aspxhome.com/file/2023/5/76005_0s.png)
Python3 适合初学者学习的银行账户登录系统实例
2021-06-16 05:09:22