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
投稿
猜你喜欢
- 作用:export和export default实现的功能相同,即:可用于导出(暴露)常量、函数、文件、模块等,以便其他文件调用。区别:1、
- 以下是我开发时碰到的问题以及心得经验,花了些时间调试,贴出来避免大家少走弯路,如有错误欢迎指正,持续更新ing xml文件尽量以
- 引言“ 这是MySQL系列笔记的第九篇,文章内容均为本人通过实践及查阅资料相关整理所得,可用作新手入门指南,或
- 1.抽象类抽象类机制中总是要定义一个公共的基类,而将特定的细节留给继承者来实现。通过抽象概念,可以在开发项目中创建扩展性很好的架构。任何一个
- 大数据时代在编程可能需要用到一些文本内容,不可能全部写到代码里,不好更改,用户也不方便使用所以需要用到我们的数据库来保存这些数据,直接更改数
- 由于工作的需求,需要用python做一个类似网络爬虫的采集器。虽然Python的urllib模块提供更加方便简洁操作,但是涉及到一些底层的需
- 具体内容如下:1 os.system例如 ipython中运行如下命令,返回运行状态statusos.system('cat /et
- 基本环境:Snow Leopard10.6.2,Oracle10.2.0.4打开Mac的终端,执行:sudo -i创建oinstall组和o
- 目录主要解决的问题一、后端返回的数据,提交到后端的数据格式如下:二、vue前端代码如下:总结主要解决的问题1、vue在循环的时候需要动态绑定
- 1、安装coveragepip install coverage安装完成后,会在Python环境下的\Scripts下看到coverage.
- hello,大家好,我是Dream。最近有小伙伴私信我,说让我出一篇海龟画图,这其实我也不太能弄得明白,那在这里我和大家一块梳理一下!记得给
- Template无疑是一个好东西,可以将字符串的格式固定下来,重复利用。同时Template也可以让开发人员可以分别考虑字符串的格式和其内容
- 先说下自己的环境,redis是部署在centos上的,爬虫运行在windows上,1. 安装redisyum install -y redi
- 1 谈谈你对面向对象的理解?面向对象的编程---object oriented programming,简称:OOP,是一种编程的思想。OO
- gRPCgRPC远程过程调用框架是基于动作的模式,类似远程调用微服务。这使得gRPC成为一种围绕Protobufs构建的进程间通信(IPC)
- 英文原文:http://www.usabilitypost.com/2009/04/15/8-characteristics-of-succ
- 前言今天在升级下载Python第三方库的时候特别慢,最后去升级pip的时候竟然还time out了,哇心态炸了。 最后想了一下为什么会这么慢
- MySQL安装分为安装版和解压版,安装版主要是由一个exe程序式安装,有界面鼠标点击安装即可,小白建议使用安装版安装mysql,相比较与安装
- 在客户端,Get方式在通过URL提交数据,数据在URL中可以看到;POST方式,数据放置在HTML HEADER内提交。GET方式提交的数据
- 本文实例为大家分享了Django实现文件上传下载的具体代码,供大家参考,具体内容如下一、django实现文件下载(1)、后台接口如果从服务器