基于tensorflow指定GPU运行及GPU资源分配的几种方式小结


Posted in Python onFebruary 03, 2020

1. 在终端执行时设置使用哪些GPU(两种方式)

(1) 如下(export 语句执行一次就行了,以后再运行代码不用执行)

基于tensorflow指定GPU运行及GPU资源分配的几种方式小结

(2) 如下

基于tensorflow指定GPU运行及GPU资源分配的几种方式小结

2. 代码中指定(两种方式)

(1)

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"

(2)

# Creates a graph.
with tf.device('/gpu:1'):
 a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
 b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
 c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print sess.run(c)

若想使用多个GPU,如下

c = []
for d in ['/gpu:0', '/gpu:1']:
 with tf.device(d):
  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])
  b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])
  c.append(tf.matmul(a, b))
with tf.device('/cpu:0'):
 sum = tf.add_n(c)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print sess.run(sum)

3.GPU资源分配

(1) 设置允许GPU增长

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config, ...)

(2) 设置每个GPU内存使用多少

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4
session = tf.Session(config=config, ...)

以上这篇基于tensorflow指定GPU运行及GPU资源分配的几种方式小结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
详解Python的Django框架中的中间件
Jul 24 Python
全面了解python字符串和字典
Jul 07 Python
python django 实现验证码的功能实例代码
May 18 Python
对变量赋值的理解--Pyton中让两个值互换的实现方法
Nov 29 Python
pandas对指定列进行填充的方法
Apr 11 Python
python实现简单http服务器功能
Sep 17 Python
python连接mongodb密码认证实例
Oct 16 Python
Python使用POP3和SMTP协议收发邮件的示例代码
Apr 16 Python
wxPython实现文本框基础组件
Nov 18 Python
Django 自动生成api接口文档教程
Nov 19 Python
Django 404、500页面全局配置知识点详解
Mar 10 Python
python下对hsv颜色空间进行量化操作
Jun 04 Python
tensorflow指定GPU与动态分配GPU memory设置
Feb 03 #Python
TensorFlow内存管理bfc算法实例
Feb 03 #Python
python numpy数组中的复制知识解析
Feb 03 #Python
opencv python Canny边缘提取实现过程解析
Feb 03 #Python
Pycharm debug调试时带参数过程解析
Feb 03 #Python
Python使用enumerate获取迭代元素下标
Feb 03 #Python
TensorFlow 显存使用机制详解
Feb 03 #Python
You might like
php 常用字符串函数总结
2008/03/15 PHP
PHP curl_setopt()函数实例代码与参数分析
2011/06/02 PHP
php curl post 时出现的问题解决
2014/01/30 PHP
PHP7 mongoDB扩展使用的方法分享
2019/05/02 PHP
js实现addClass,removeClass,hasClass的函数代码
2011/07/13 Javascript
jquery 滚动条事件简单实例
2013/07/12 Javascript
移动端JQ插件hammer使用详解
2015/07/03 Javascript
jQuery生成假加载动画效果
2016/12/01 Javascript
jquery 删除节点 添加节点 找兄弟节点的简单实现
2016/12/07 Javascript
JS实现数组简单去重及数组根据对象中的元素去重操作示例
2018/01/05 Javascript
vue页面切换到滚动页面显示顶部的实例
2018/03/13 Javascript
vue中实现上传文件给后台实例详解
2019/08/22 Javascript
Vue获取微博授权URL代码实例
2020/11/04 Javascript
Python实现list反转实例汇总
2014/11/11 Python
python根据给定文件返回文件名和扩展名的方法
2015/03/27 Python
浅谈python中的面向对象和类的基本语法
2016/06/13 Python
python 异常处理总结
2016/10/18 Python
Python实现读取json文件到excel表
2017/11/18 Python
Python PyAutoGUI模块控制鼠标和键盘实现自动化任务详解
2018/09/04 Python
Python类中的魔法方法之 __slots__原理解析
2019/08/26 Python
python检测服务器端口代码实例
2019/08/31 Python
Python使用__new__()方法为对象分配内存及返回对象的引用示例
2019/09/20 Python
Python使用OpenPyXL处理Excel表格
2020/07/02 Python
SneakerStudio英国:最佳运动鞋商店
2019/05/22 全球购物
电子商务专业实习生自我鉴定
2013/09/24 职场文书
课堂教学改革实施方案
2014/03/17 职场文书
公司承诺书范文
2014/05/19 职场文书
酒店开业庆典策划方案
2014/05/28 职场文书
素质教育标语
2014/06/27 职场文书
英语三分钟演讲稿
2014/08/19 职场文书
幼儿园家长工作总结2015
2015/04/25 职场文书
海上钢琴师观后感
2015/06/03 职场文书
郭明义观后感
2015/06/08 职场文书
创业计划书之婴幼儿游泳馆
2019/09/11 职场文书
python中if和elif的区别介绍
2021/11/07 Python
详解Spring Security中的HttpBasic登录验证模式
2022/03/17 Java/Android