支持的设备

在一套标准的系统上通常有多个计算设备. TensorFlow 支持 CPU 和 GPU 这两种设备. 我们用指定字符串strings 来标识这些设备. 比如:

  • "/cpu:0": 机器中的 CPU
  • "/gpu:0": 机器中的 GPU, 如果你有一个的话.
  • "/gpu:1": 机器中的第二个 GPU, 以此类推...

如果一个 TensorFlow 的 operation 中兼有 CPU 和 GPU 的实现, 当这个算子被指派设备时, GPU 有优先权. 比如matmul中 CPU 和 GPU kernel 函数都存在. 那么在 cpu:0 和 gpu:0 中, matmul operation 会被指派给 gpu:0 .

记录设备指派情况

为了获取你的 operations 和 Tensor 被指派到哪个设备上运行, 用 log_device_placement 新建一个 session, 并设置为 True.

# 新建一个 graph.
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)
# 新建session with log_device_placement并设置为True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# 运行这个 op.
print sess.run(c)

你应该能看见以下输出:

Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus
id: 0000:05:00.0
b: /job:localhost/replica:0/task:0/gpu:0
a: /job:localhost/replica:0/task:0/gpu:0
MatMul: /job:localhost/replica:0/task:0/gpu:0
[[ 22.  28.][ 49.  64.]]

手工指派设备

如果你不想使用系统来为 operation 指派设备, 而是手工指派设备, 你可以用 with tf.device 创建一个设备环境, 这个环境下的 operation 都统一运行在环境指定的设备上.

# 新建一个graph.
with tf.device('/cpu:0'):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)
# 新建session with log_device_placement并设置为True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# 运行这个op.
print sess.run(c)

你会发现现在 a 和 b 操作都被指派给了 cpu:0.

Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus
id: 0000:05:00.0
b: /job:localhost/replica:0/task:0/cpu:0
a: /job:localhost/replica:0/task:0/cpu:0
MatMul: /job:localhost/replica:0/task:0/gpu:0
[[ 22.  28.][ 49.  64.]]

在多GPU系统里使用单一GPU

如果你的系统里有多个 GPU, 那么 ID 最小的 GPU 会默认使用. 如果你想用别的 GPU, 可以用下面的方法显式的声明你的偏好:

# 新建一个 graph.
with tf.device('/gpu:2'):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)
# 新建 session with log_device_placement 并设置为 True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# 运行这个 op.
print sess.run(c)

如果你指定的设备不存在, 你会收到 InvalidArgumentError 错误提示:

InvalidArgumentError: Invalid argument: Cannot assign a device to node 'b':
Could not satisfy explicit device specification '/gpu:2'[[Node: b = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [3,2]values: 1 2 3...>, _device="/gpu:2"]()]]

为了避免出现你指定的设备不存在这种情况, 你可以在创建的 session 里把参数 allow_soft_placement 设置为 True, 这样 tensorFlow 会自动选择一个存在并且支持的设备来运行 operation.

# 新建一个 graph.
with tf.device('/gpu:2'):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)
# 新建 session with log_device_placement 并设置为 True.
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
# 运行这个 op.
print sess.run(c)

使用多个 GPU

如果你想让 TensorFlow 在多个 GPU 上运行, 你可以建立 multi-tower 结构, 在这个结构 里每个 tower 分别被指配给不同的 GPU 运行. 比如:

# 新建一个 graph.
c = []
for d in ['/gpu:2', '/gpu:3']: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)
# 新建session with log_device_placement并设置为True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# 运行这个op.
print sess.run(sum)

你会看到如下输出:

Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K20m, pci bus
id: 0000:02:00.0
/job:localhost/replica:0/task:0/gpu:1 -> device: 1, name: Tesla K20m, pci bus
id: 0000:03:00.0
/job:localhost/replica:0/task:0/gpu:2 -> device: 2, name: Tesla K20m, pci bus
id: 0000:83:00.0
/job:localhost/replica:0/task:0/gpu:3 -> device: 3, name: Tesla K20m, pci bus
id: 0000:84:00.0
Const_3: /job:localhost/replica:0/task:0/gpu:3
Const_2: /job:localhost/replica:0/task:0/gpu:3
MatMul_1: /job:localhost/replica:0/task:0/gpu:3
Const_1: /job:localhost/replica:0/task:0/gpu:2
Const: /job:localhost/replica:0/task:0/gpu:2
MatMul: /job:localhost/replica:0/task:0/gpu:2
AddN: /job:localhost/replica:0/task:0/cpu:0
[[  44.   56.][  98.  128.]]

TensorFlow 教程 --进阶指南--3.8使用GPU相关推荐

  1. TensorFlow 教程 --进阶指南--3.1总览

    综述 Overview Variables: 创建,初始化,保存,和恢复 TensorFlow Variables 是内存中的容纳 tensor 的缓存.这一小节介绍了用它们在模型训练时(during ...

  2. TensorFlow 教程 --进阶指南--3.9TensorBoard: 图表可视化

    TensorFlow 图表计算强大而又复杂,图表可视化在理解和调试时显得非常有帮助. 下面是一个运作时的可式化例子. "一个TensorFlow图表的可视化") 一个TensorF ...

  3. TensorFlow 教程 --进阶指南--3.10共享变量

    你可以在怎么使用变量中所描述的方式来创建,初始化,保存及加载单一的变量.但是当创建复杂的模块时,通常你需要共享大量变量集并且如果你还想在同一个地方初始化这所有的变量,我们又该怎么做呢.本教程就是演示如 ...

  4. TensorFlow 教程 --进阶指南--3.6增加一个新 Op

    预备知识: 对 C++ 有一定了解. 已经下载 TensorFlow 源代码并有能力编译它. 如果现有的库没有涵盖你想要的操作, 你可以自己定制一个. 为了使定制的 Op 能够兼容原有的库 , 你必须 ...

  5. TensorFlow 教程 --进阶指南--3.4数据读取

    数据读取 TensorFlow程序读取数据一共有3种方法: 供给数据(Feeding): 在TensorFlow程序运行的每一步, 让Python代码来供给数据. 从文件读取数据: 在TensorFl ...

  6. TensorFlow 教程 --进阶指南--3.7自定义数据读取

    基本要求: 熟悉 C++ 编程. 确保下载 TensorFlow 源文件, 并可编译使用. 我们将支持文件格式的任务分成两部分: 文件格式: 我们使用 Reader Op来从文件中读取一个 recor ...

  7. TensorFlow 教程 --进阶指南--3.5线程和队列

    在使用TensorFlow进行异步计算时,队列是一种强大的机制. 正如TensorFlow中的其他组件一样,队列就是TensorFlow图中的节点.这是一种有状态的节点,就像变量一样:其他节点可以修改 ...

  8. TensorFlow 教程 --进阶指南--3.3可视化学习

    TensorBoard:可视化学习 TensorBoard 涉及到的运算,通常是在训练庞大的深度神经网络中出现的复杂而又难以理解的运算. 为了更方便 TensorFlow 程序的理解.调试与优化,我们 ...

  9. TensorFlow 教程 --进阶指南--3.2变量:创建、初始化、保存和加载

    变量:创建.初始化.保存和加载 当训练模型时,用变量来存储和更新参数.变量包含张量 (Tensor)存放于内存的缓存区.建模时它们需要被明确地初始化,模型训练后它们必须被存储到磁盘.这些变量的值可在之 ...

最新文章

  1. k8s 命令 重启_快速入门Kubernetes(K8S)——资源清单
  2. html5 呼吸灯效果,jQuery仿地铁线路指示灯效果
  3. 理解cookie与token(JWT)
  4. hprofile教程
  5. Android selector中的item的顺序
  6. 设计模式之禅--思维导图
  7. python中type(12.34)_下面代码的输出结果是
  8. 【青草识别】基于matlab GUI形态学马唐草+牛筋草识别【含Matlab源码 1041期】
  9. 按键精灵手机助手之实战篇(一)需求理论分析
  10. 如何查看、检测Windows XP-Windows10系统是否为正版
  11. 查看linux内存和硬盘
  12. 【HTML——盛开花朵】(效果+代码)
  13. 【荷露叮咚网络学苑】人人需具备的基本信息素养视频录制完成
  14. StarSpace系列之一:tagspace
  15. 启信宝发布植树节产业洞察:超2000家绿色造纸企业,造纸业迭代落后产能
  16. springCloud集成nacos
  17. Oracle EBS财务模块(二)基本组成模块
  18. Deep Residual Learning for Image Recognition浅读与实现
  19. MQ消息队列数据丢失问题
  20. 在Windows上安装Elasticsearch 5.x

热门文章

  1. 等待队列中为什么需要互斥锁?一个线程在等待时被唤醒后会做什么?安全队列的代码实现
  2. 兴利调节matlab编程,水利计算之兴利调节计算.ppt
  3. qt mysql分页控件_Qt分页布局与切分窗口的实现
  4. linux程序ctrl c无法退出原因_「Linux」20节-在后台运行任务
  5. sql oracle 递归查询语句,深入sql oracle递归查询
  6. 【重难点】【JUC 03】怎么实现一个线程安全的队列、手写模拟实现一个阻塞队列
  7. 力扣628. 三个数的最大乘积
  8. 力扣645.错误的集合
  9. ios Image裁剪成圆形的方法
  10. 踩坑:VScode 集成 eslint 插件