一、TensorFlow 设备分配

1、设备分配规则

If a TensorFlow operation has both CPU and GPU implementations, the GPU devices will be given priority when the operation is assigned to a device.

2、手动指定设备分配

  • 如果你不想让系统自动为 operation 分配设备, 而是自己手动指定, 可以用 with tf.device 创建一个设备环境, 这个环境下的 operation 都统一运行在指定的设备上.
  • 代码示例如下:
 1 # op 在 cpu 上运算
2 with tf.device('/cpu:0'):
3 a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
4 b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
5
6 # op 在 gpu 上运算
7 with tf.device('/device:GPU:2'):
8 a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
9 b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
10
11 # op 在 gpus 上运算
12 for d in ['/device:GPU:2', '/device:GPU:3']:
13 with tf.device(d):
14 a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])
15 b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])

二、TensorFlow GPU 配置

1、指定可以被看见的GPU设备

1 import os
2
3 # 默认情况,TF 会占用所有 GPU 的所有内存, 我们可以指定
4 # 只有 GPU0 和 GPU1 这两块卡被看到,从而达到限制其使用所有GPU的目的
5 os.environ['CUDA_VISIBLE_DEVICES'] = '0, 1'
6
7 # 打印 TF 可用的 GPU
8 print os.environ['CUDA_VISIBLE_DEVICES']
9 >>> 0, 1

 2、限定使用显存的比例

 1 # 在开启对话session前,先创建一个 tf.ConfigProto() 实例对象
2 # 通过 allow_soft_placement 参数自动将无法放在 GPU 上的操作放回 CPU
3 gpuConfig = tf.ConfigProto(allow_soft_placement=True)
4
5 # 限制一个进程使用 60% 的显存
6 gpuConfig.gpu_options.per_process_gpu_memory_fraction = 0.6
7
8 # 把你的配置部署到session
9 with tf.Session(config=gpuConfig) as sess:
10 pass
11
12 这样,如果你指定的卡的显存是8000M的话,你这个进程只能用4800M。

3、需要多少拿多少

 1 # 在开启对话session前,先创建一个 tf.ConfigProto() 实例对象
2 # 通过 allow_soft_placement 参数自动将无法放在 GPU 上的操作放回 CPU
3 gpuConfig = tf.ConfigProto(allow_soft_placement=True)
4
5 # 运行时需要多少再给多少
6 gpuConfig.gpu_options.allow_growth = True
7
8 # 把你的配置部署到session
9 with tf.Session(config=gpuConfig) as sess:
10 pass

  4、GPU 使用总结

1 import os
2 os.environ['CUDA_VISIBLE_DEVICES'] = '0, 1'
3
4 gpuConfig = tf.ConfigProto(allow_soft_placement=True)
5 gpuConfig.gpu_options.allow_growth = True
6
7 with tf.Session(config=gpuConfig) as sess:
8 pass

TensorFlow GPU 的使用的更多相关文章

  1. 【转】Ubuntu 16.04安装配置TensorFlow GPU版本

    之前摸爬滚打总是各种坑,今天参考这篇文章终于解决了,甚是鸡冻\(≧▽≦)/,电脑不知道怎么的,安装不了16.04,就安装15.10再升级到16.04 requirements: Ubuntu 16.0 ...

  2. Ubuntu 16.04 + CUDA 8.0 + cuDNN v5.1 + TensorFlow(GPU support)安装配置详解

    随着图像识别和深度学习领域的迅猛发展,GPU时代即将来临.由于GPU处理深度学习算法的高效性,使得配置一台搭载有GPU的服务器变得尤为必要. 本文主要介绍在Ubuntu 16.04环境下如何配置Ten ...

  3. 备注: ubt 16.04 安装 gtx 1060 --- 成功运行 tensorflow - gpu

    ---------------------------------------------------------------------------------------------------- ...

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

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

  5. Win10上安装Keras 和 TensorFlow(GPU版本)

    一. 安装环境 Windows 10 64bit  家庭版 GPU: GeForce GTX1070 Python: 3.5 CUDA: CUDA Toolkit 8.0 GA1 (Sept 2016 ...

  6. Win10 x64 + CUDA 10.0 + cuDNN v7.5 + TensorFlow GPU 1.13 安装指南

    Win10 x64 + CUDA 10.0 + cuDNN v7.5 + TensorFlow GPU 1.13 安装指南 Update : 2019.03.08 0. 环境说明 硬件:Ryzen R ...

  7. windows安装tensorflow GPU

    一.安装Anaconda Anaconda是Python发行包,包含了很多Python科学计算库.它是比直接安装Python更好的选择. 二.安装Tensorflow 如果安装了tensorflow, ...

  8. TensorFlow DeepLab教程初稿-tensorflow gpu安装教程

    TensorFlow DeepLab教程初稿-tensorflow gpu安装教程 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com Summar ...

  9. 记录从裸机到TensorFlow GPU版运行 的配置过程

    实验室原来有一台装Ubuntu Server系统的服务器,安装有tensorflow,在使用过程中经常出现断网.死机.自动关机等毛病,忍无可忍,决定重装系统 配置如下:Dell工作站,Xeon-E5 ...

随机推荐

  1. [C++] printf pitfall

    printf pitfal l

  2. PollingProvider方法的使用及示例

    来自<sencha touch权威指南>第12章,374页开始 ----------------------------------------------------- PollingP ...

  3. 如何优雅地使用命令行设置windows文件关联

    如何优雅地使用命令行设置windows文件关联 使用ftype查看帮助 设置关联所需命令有ftype assoc,需要管理员权限.如果忘记使用方法可通过ftype的帮助获取查看方法 C:\WINDOW ...

  4. python nose的html报告优化

    用的是nose的nose-html-reporting (0.2.3)插件生成报告.用了bootstrap前端框架,加入了开始时间和计算持续时间,及其本地化. 优化后的显示效果: 代码地址

  5. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(12):XML配置自动扫描包,自动加载*.properties文件

    一.XML和注解组合使用 前几篇的测试案例都是在Java类中配置,现在换一种使用方式,在XML中配置,使Spring IoC容器在启动之后自动去扫描配置的包路径,扫描加载指定路径下的propertie ...

  6. WSAStartup函数

    函数WSAStartup 一.WSAStartup函数                 int WSAStartup                       (                   ...

  7. 关于validate的自定义样式

    $("#Form").validate({ rules: { name: "required" }, messages: { name: "请输入您的 ...

  8. 通过python实现wc基本功能

    ---恢复内容开始--- 1.Github项目地址: https://github.com/zhg1998/ww/blob/master/wc.py 2.项目相关要求: 写一个命令行程序,模仿已有wc ...

  9. C/C++文件输入输出流

        C++方式 C方式 头文件   fstream stdio.h open   file.open(const char *filename,const char *mode) FILE* fo ...

  10. solr的schema.xml配置属性解释

    schema.xml做什么? SOLR加载数据,创建索引和数据时,核心数据结构的配置文件是schema.xml,该配置文件主要用于配置数据源,字段类型定义,搜索类型定义等.schema.xml的配置直 ...