tensorflow(GPU)使用
一,直接指定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)使用相关推荐
- python 虚拟环境 tensorflow GPU
拿到一个新的容器之后,怎么创建一个独立的GPU训练环境呢?之前弄的时候总是零零散散的,现在把它总结在这里,供自己以及有需要的朋友查阅. conda创建 1.1 下载anaconda wget -c h ...
- tensorflow GPU 内存不够
tensorflow GPU 内存不够 from tensorflow as tf gpus = tf.config.list_physical_devices('GPU') if gpus:try: ...
- tensorflow GPU python cuda cudnn 匹配
tensorflow GPU 匹配 https://tensorflow.google.cn/install/source_windows?hl=zh_cn
- Windows Tensorflow GPU安装
GPU资源对神经网络模型的训练很重要,应充分利用电脑的显卡资源,加快模型的训练速度.这里是本人安装tensorflow-gpu的过程,记录了安装的步骤以及在每个步骤中参考的资料以及所遇到的坑. 大体步 ...
- conda安装tensorflow-gpu简洁版_笔记本的垃圾显卡也能装Tensorflow GPU版,简明教程
有图有真相,先看最后安装效果 首先关于硬性条件,Tensorflow要求的条件如下: CUDA® 计算能力为 3.5 或更高的 NVIDIA® GPU 卡 这是什么概念呢?在英伟达官方给出的显卡算力列 ...
- 通过Anaconda在Ubuntu16.04上安装 TensorFlow(GPU版本)
一. 安装环境 Ubuntu16.04.3 LST GPU: GeForce GTX1070 Python: 3.5 CUDA Toolkit 8.0 GA1 (Sept 2016) cuDNN v6 ...
- tensorflow gpu python3.5_Win10+Anaconda3下tensorflow-gpu环境配置
Win10+Anaconda3下tensorflow-gpu环境配置 基本环境 操作系统window10-education 显卡 NIVIDA GETFORCE GTX 1050 安装Anacond ...
- linux安装tensorflow教程,Ubuntu 16.04 安装 TensorFlow(GPU支持)
本文记录Ubuntu 16.04安装Tensorflow步骤,也包括怎么从源码编译安装Tensorflow. 要想安装Tensorflow GPU版本,你需要有一个新一点的Nvidia显卡. Tens ...
- 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 ...
- 【tensorflow】tensorflow -gpu安装及jupyter环境更改
tensorflow -gpu安装 首先,安装Anoconda 1. 官网下载点我: 2.安装 点击 python 3.6 version自动下载x64版,下载好之后,然后安装. 如图,打上勾之后,一 ...
最新文章
- 百行代码打造一个DI容器(支持瞬时生命周期、单利生命周期、构造函数自动注入、属性自动注入、字段自动注入)...
- 在屏幕上输出你好的python语句是_编程实现:在屏幕上输出中文字符“你好,世界”。(输出结果中不带双引号)_学小易找答案...
- python教程:mixin详解
- 利用license机制来保护Java软件产品的安全
- unet实现区域分割
- 关于php编译安装扩展模块memcache的问题
- 操作系统及IIS版本选择参考
- 汇编语言指令大全(详细)
- 电路基本原理的那些事儿之 分压原理
- 2022道路运输企业安全生产管理人员考试练习题及在线模拟考试
- SpringSecurity实战(四)-集成图片验证码-过滤器方式实现
- 爱荷华大学计算机科学专业,爱荷华大学计算机科学本科.pdf
- android 加载第三方so文件,Uni-app 以Module方式开发Android插件,引入第三方资源包so文件,但无法读取...
- 1013. Battle Over Cities (25)
- 浙江大学的计算机考研难度,浙江大学部分专业考研难度分析
- Valley Blue Pasture《谷蓝尼牧场》签到可领大红包
- 聊城大学计算机学院许丽莉,计算机学院
- Redis_保存数据时报错MISCONF Redis is configured to save RDB snapshots, but it is curren
- Vue学习——Uncaught TypeError: “i“ is read-only
- cPanel主机空间安装Magento规范准确详细教程