Tensorflow全局设置可见GPU编号操作
作者:silent56_th 发布时间:2021-04-21 12:41:46
标签: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)
来源:https://blog.csdn.net/silent56_th/article/details/81319597
0
投稿
猜你喜欢
- 有时候我们需要关闭print输出信息,我们可以通过控制sys.stdout来实现print输出的开关一个简单的示例如下:import sys
- urls.py:URL dispatcher(路由配置文件)URL配置(URLconf)就像是Django所支撑网站的目录。它的本质是URL
- 查看表空间的名称及大小代码如下:SQL>select t.tablespace_name, round(sum(bytes/(1024
- 本文实例讲述了Python tkinter事件高级用法。分享给大家供大家参考,具体如下:先来看看运行效果:完整实例代码:# -*- codi
- 写下这篇博客,起源于Tornado邮件群组的这个问题how to use outer variable in inner method,这里
- 天气真的是越来越冷啦,有时候我们想翻看网页新闻,但是又冷的不想把手拿出来,移动鼠标翻看。这时候,是不是特别想电脑像讲故事一样,给我们念出来呢
- 众所周知,Python开发框架大大减少了开发者不必要的重复劳动,提高了项目开发效率的同时,还使得创建的程序更加稳定。目前比较主流的Pytho
- 由于javascript无法获取img文件头数据,必须等待其加载完毕后才能获取真实的大小,所以lightbox类效果为了让图片居中显示,导致
- 让我们重温一下JavaScript的一些基础知识,请先写出以下代码中问号处的答案,再运行比较!<script type=&q
- 本文实例讲述了html静态页面调用php文件的方法。分享给大家供大家参考。具体方法如下:静态页面中看上去好像是不能直接调用php文件的,但是
- Python continue语句返回while循环的开始。Continue语句拒绝在该循环的当前迭代中的其余语句执行并移动控制
- 看代码吧~import numpy as npa = np.array([[2, 4, 6, 1], [1, 5, 2, 9]])print
- 在Python中,split() 方法可以实现将一个字符串按照指定的分隔符切分成多个子串,这些子串会被保存到列表中(不包含分隔符),作为方法
- 核心代码:#!/usr/bin/python#-*- coding:gbk -*-#设置源文件输出格式import sysimport ge
- 现在用python画图已经难不倒一直跟小编学习的小伙伴们了,甚至有的小伙伴画图比小编还要厉害。为此小编还偷偷下了一番功夫,画图这种事情,细节
- 实验目的:验证主动释放内存变量是否有价值. 实验原始代码: <script language=vbscript runat=serve
- 反射指的是运行时动态的获取变量的相关信息1. reflect 包类型是变量,类别是常量reflect.TypeOf,获取变量的类型,返回re
- NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供
- 本文实例为大家分享了python实现飞机大战的具体代码,供大家参考,具体内容如下实现的效果如下: 主程序代码如下:import p
- 这篇博客其实就是这个集合整理后一部分的公开亮相。如果你已经是个python大牛,那么基本上你应该知道这里面的大多数用法了,但我想你应该也能发