一,直接指定GPU:

tf.ConfigProto一般用在创建session的时候。用来对session进行参数配置

with tf.Session(config = tf.ConfigProto(...),...)
#tf.ConfigProto()的参数
log_device_placement=True : 是否打印设备分配日志
allow_soft_placement=True : 如果你指定的设备不存在,允许TF自动分配设备
#coding:utf-8
import tensorflow as tf
a=tf.constant([1.0,2.0,3.0],shape=[3],name='a')
b=tf.constant([1.0,2.0,3.0],shape=[3],name='b')
c=a+b
#log_device_placement=True 输出运行每一个运算的设备
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:print(sess.run(c))

可看出每个运算的设备是GPU:0

可以把运算放在不同的device上
#coding:utf-8
import tensorflow as tf
with tf.device('/cpu:0'):a=tf.constant([1.0,2.0,3.0],shape=[3],name='a')b=tf.constant([1.0,2.0,3.0],shape=[3],name='b')
with tf.device('/gpu:0'):c=a+b
#log_device_placement=True 输出运行每一个运算的设备
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:print(sess.run(c))

直接指定的缺点是,降低程序的可移植性,同时有些运算不能在GPU上执行,bi比如tf.Variable操作只支持实数型(float16,float32,和double的参数),对于整数不支持,故可用allow_soft_placement参数

报错:

a_cpu=tf.Variable(0,name='a_cpu')
with tf.device('/gpu:0'):a_gpu=tf.Variable(0,name='a_gpu')
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:print(sess.run(tf.global_variables_initializer()))

a_cpu=tf.Variable(0,name='a_cpu')
with tf.device('/gpu:0'):a_gpu=tf.Variable(0,name='a_gpu')
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,log_device_placement=True)) as sess:print(sess.run(tf.global_variables_initializer()))

打印信息:

2018-08-30 03:40:26.905925: I tensorflow/core/common_runtime/placer.cc:874] a_gpu: (VariableV2)/job:localhost/replica:0/task:0/device:CPU:0
a_gpu/read: (Identity): /job:localhost/replica:0/task:0/device:CPU:0
2018-08-30 03:40:26.905952: I tensorflow/core/common_runtime/placer.cc:874] a_gpu/read: (Identity)/job:localhost/replica:0/task:0/device:CPU:0
a_gpu/Assign: (Assign): /job:localhost/replica:0/task:0/device:CPU:0

import tensorflow as tf
a=tf.constant([1.0,2.0,3.0],shape=[3],name='a')
b=tf.constant([1.0,2.0,3.0],shape=[3],name='b')c=a+b
#log_device_placement=True 输出运行每一个运算的设备
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:print(sess.run(c))

二,命令行用gpu

打印信息

2018-08-30 03:46:25.130842: I tensorflow/core/common_runtime/placer.cc:874] add: (Add)/job:localhost/replica:0/task:0/device:GPU:0
b: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2018-08-30 03:46:25.130872: I tensorflow/core/common_runtime/placer.cc:874] b: (Const)/job:localhost/replica:0/task:0/device:GPU:0
a: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2018-08-30 03:46:25.130892: I tensorflow/core/common_runtime/placer.cc:874] a: (Const)/job:localhost/replica:0/task:0/device:GPU:0
运算在第二块gpu

或者直接在程序里

import tensorflow as tf
import os
os.environ['CUDA_VISIBLE_DEVICES']='1'
a=tf.constant([1.0,2.0,3.0],shape=[3],name='a')
b=tf.constant([1.0,2.0,3.0],shape=[3],name='b')c=a+b
#log_device_placement=True 输出运行每一个运算的设备
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:print(sess.run(c))

三,动态分配GPU

上面的方式会占用整个GPU,可以在一块GPU上同时运行多个任务

import tensorflow as tf
import os
def set_config():# 控制使用率os.environ['CUDA_VISIBLE_DEVICES'] = '1'# 假如有16GB的显存并使用其中的8GB:gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)config = tf.ConfigProto(gpu_options=gpu_options)# session = tf.Session(config=config)return configif __name__ == '__main__':a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')c = a + bcfg=set_config()with tf.Session(config=cfg) as sess:print(sess.run(c))

tensorflow(GPU)使用相关推荐

  1. python 虚拟环境 tensorflow GPU

    拿到一个新的容器之后,怎么创建一个独立的GPU训练环境呢?之前弄的时候总是零零散散的,现在把它总结在这里,供自己以及有需要的朋友查阅. conda创建 1.1 下载anaconda wget -c h ...

  2. tensorflow GPU 内存不够

    tensorflow GPU 内存不够 from tensorflow as tf gpus = tf.config.list_physical_devices('GPU') if gpus:try: ...

  3. tensorflow GPU python cuda cudnn 匹配

    tensorflow GPU 匹配 https://tensorflow.google.cn/install/source_windows?hl=zh_cn

  4. Windows Tensorflow GPU安装

    GPU资源对神经网络模型的训练很重要,应充分利用电脑的显卡资源,加快模型的训练速度.这里是本人安装tensorflow-gpu的过程,记录了安装的步骤以及在每个步骤中参考的资料以及所遇到的坑. 大体步 ...

  5. conda安装tensorflow-gpu简洁版_笔记本的垃圾显卡也能装Tensorflow GPU版,简明教程

    有图有真相,先看最后安装效果 首先关于硬性条件,Tensorflow要求的条件如下: CUDA® 计算能力为 3.5 或更高的 NVIDIA® GPU 卡 这是什么概念呢?在英伟达官方给出的显卡算力列 ...

  6. 通过Anaconda在Ubuntu16.04上安装 TensorFlow(GPU版本)

    一. 安装环境 Ubuntu16.04.3 LST GPU: GeForce GTX1070 Python: 3.5 CUDA Toolkit 8.0 GA1 (Sept 2016) cuDNN v6 ...

  7. tensorflow gpu python3.5_Win10+Anaconda3下tensorflow-gpu环境配置

    Win10+Anaconda3下tensorflow-gpu环境配置 基本环境 操作系统window10-education 显卡 NIVIDA GETFORCE GTX 1050 安装Anacond ...

  8. linux安装tensorflow教程,Ubuntu 16.04 安装 TensorFlow(GPU支持)

    本文记录Ubuntu 16.04安装Tensorflow步骤,也包括怎么从源码编译安装Tensorflow. 要想安装Tensorflow GPU版本,你需要有一个新一点的Nvidia显卡. Tens ...

  9. Tensorflow GPU安装指南 (Ubuntu 16.04 anaconda cuda8.0 cuDNN6.0)

    1. 安装python python环境使用anaconda 从官方网站下载操作系统对应的版本 chmod +x Anaconda3-5.0.1-Linux-x86_64.sh ./Anaconda3 ...

  10. 【tensorflow】tensorflow -gpu安装及jupyter环境更改

    tensorflow -gpu安装 首先,安装Anoconda 1. 官网下载点我: 2.安装 点击 python 3.6 version自动下载x64版,下载好之后,然后安装. 如图,打上勾之后,一 ...

最新文章

  1. 百行代码打造一个DI容器(支持瞬时生命周期、单利生命周期、构造函数自动注入、属性自动注入、字段自动注入)...
  2. 在屏幕上输出你好的python语句是_编程实现:在屏幕上输出中文字符“你好,世界”。(输出结果中不带双引号)_学小易找答案...
  3. python教程:mixin详解
  4. 利用license机制来保护Java软件产品的安全
  5. unet实现区域分割
  6. 关于php编译安装扩展模块memcache的问题
  7. 操作系统及IIS版本选择参考
  8. 汇编语言指令大全(详细)
  9. 电路基本原理的那些事儿之 分压原理
  10. 2022道路运输企业安全生产管理人员考试练习题及在线模拟考试
  11. SpringSecurity实战(四)-集成图片验证码-过滤器方式实现
  12. 爱荷华大学计算机科学专业,爱荷华大学计算机科学本科.pdf
  13. android 加载第三方so文件,Uni-app 以Module方式开发Android插件,引入第三方资源包so文件,但无法读取...
  14. 1013. Battle Over Cities (25)
  15. 浙江大学的计算机考研难度,浙江大学部分专业考研难度分析
  16. Valley Blue Pasture《谷蓝尼牧场》签到可领大红包
  17. 聊城大学计算机学院许丽莉,计算机学院
  18. Redis_保存数据时报错MISCONF Redis is configured to save RDB snapshots, but it is curren
  19. Vue学习——Uncaught TypeError: “i“ is read-only
  20. cPanel主机空间安装Magento规范准确详细教程

热门文章

  1. DIN+DIEN,机器学习唯一指定涨点技Attention
  2. 如何匹配两段文本的语义?
  3. 人工智能大地图之分布式人工智能篇
  4. 参加完阿里蚂蚁金服Java中间件6轮面试题!6点血泪总结~
  5. 论文浅尝 | Data Intelligence第4期正式上线啦
  6. 论文浅尝 | 时序与因果关系联合推理
  7. 机器学习十大经典算法之岭回归和LASSO回归
  8. Linux下的Tomcat服务器修改server.xml中的8080端口号后出现不能访问首页
  9. 关于表情符号与UTF-8的探讨
  10. 是否同一棵二叉搜索树