tensorflow使用gpu
来源:互联网 发布:深圳24小时营业 知乎 编辑:程序博客网 时间:2024/06/09 22:26
支持的设备
在一套标准的系统上通常有多个计算设备. 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
新建一个 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.]]
cifar10 tutorial 这个例子很好的演示了怎样用GPU集群训练.
- tensorflow gpu使用情况
- tensorflow:GPU使用
- tensorflow GPU使用问题
- tensorflow 使用ConfigProto&GPU
- tensorflow使用gpu
- 【tensorflow 学习】 gpu使用
- tensorflow 不使用GPU 无法利用GPU
- Tensorflow指定使用哪个GPU
- 指定TensorFlow使用哪一个GPU
- 17、TensorFLow GPU 的使用
- tensorflow指定gpu的使用
- Tensorflow 多GPU下,指定使用某一块GPU
- tensorflow中使用指定的GPU及GPU显存
- tensorflow-gpu
- Keras设定GPU使用内存大小(Tensorflow backend)
- Keras以及Tensorflow强制使用CPU,GPU
- Keras设定GPU使用内存大小(Tensorflow backend)
- Theano和Tensorflow多GPU使用问题
- Kafka使用log.retention.hours改变消息端的消息保存时间
- $.post() 提交表单,$.get() 异步刷新页面
- VM虚拟机centos6.5安装nginx
- HTML表格,跨行、跨列
- Qt插件定义
- tensorflow使用gpu
- Java之Javassist对类的常用操作小结-yellowcong
- Spring Boot相关知识
- 下列代码之后的结果为()?
- REM手机屏幕适配
- jdbc连接数据库的7大步骤(以通过jdbc连接实现数据的插入、更新为例)
- Twitter_Snowflake生成主键代码
- EPON OLT网管系统的实现
- 破解 Navicat Premium 12