tensorflow 在指定CPU/GPU上运行方法
2017-07-20 16:48
477 查看
转自:http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/using_gpu.html
在一套标准的系统上通常有多个计算设备. TensorFlow 支持 CPU 和 GPU 这两种设备. 我们用指定字符串
如果一个 TensorFlow 的 operation 中兼有 CPU 和 GPU 的实现, 当这个算子被指派设备时, GPU 有优先权. 比如
GPU kernel 函数都存在. 那么在
会被指派给
为了获取你的 operations 和 Tensor 被指派到哪个设备上运行, 用
并设置为
你应该能看见以下输出:
如果你不想使用系统来为 operation 指派设备, 而是手工指派设备, 你可以用
都统一运行在环境指定的设备上.
你会发现现在
如果你的系统里有多个 GPU, 那么 ID 最小的 GPU 会默认使用. 如果你想用别的 GPU, 可以用下面的方法显式的声明你的偏好:
如果你指定的设备不存在, 你会收到
为了避免出现你指定的设备不存在这种情况, 你可以在创建的
这样 tensorFlow 会自动选择一个存在并且支持的设备来运行 operation.
如果你想让 TensorFlow 在多个 GPU 上运行, 你可以建立 multi-tower 结构, 在这个结构 里每个 tower 分别被指配给不同的 GPU 运行. 比如:
你会看到如下输出:
cifar10 tutorial 这个例子很好的演示了怎样用GPU集群训练.
使用 GPUs
支持的设备
在一套标准的系统上通常有多个计算设备. 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中,
matmuloperation
会被指派给
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.]]
cifar10 tutorial 这个例子很好的演示了怎样用GPU集群训练.
相关文章推荐
- TensorFlow安装方法三【Anaconda方式下查看TensorFlow版本选择性安装】(Windows10 64位 cpu and gpu)
- Ubuntu下tensorflow-gpu只能在CPU上运行的解决方案
- windows下实现特定进程在指定的逻辑cpu下运行的方法.
- TensorFlow安装方法二【第一步:GPU环境配置部分(装CPU版跳过直接看第二步)】(Windows10 64位 )
- 【TensorFlow】Windows环境下PyCharm运行TensorFlow GPU版(附TensorFlow更新方法)
- tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定
- Tensorflow 指定GPU运行
- Visual Studio .NET 已检测到指定的Web服务器运行的不是ASP.NET 1.1 版。您将无法运行ASP.NET Web应用程序或服务。 ---解决方法
- wince6.0下将应用程序放到指定存储设备下运行的方法
- Tensorflow 多GPU下,指定使用某一块GPU
- 用maven运行指定java类main方法
- tensorflow运行错误及解决方法--一直更新
- 在linux下查看指定进程的所运行的CPU核
- linux:如何指定进程运行的CPU
- linux shell 查看进程运行或绑定的cpu和绑定执行进程到特定cpu上执行的方法
- tensorflow_gpu(cpu)+keras for windows 配置过程
- Win10下安装及运行tensorflow(CPU版)(五)——安装Keras以及opencv
- Tensorflow Ubuntu16.04上安装及CPU运行Tensorboard、CNN、RNN图文教程(转载)
- 创建运行时类的对象,调用指定的属性方法构造器
- “Visual Studio.net已检测到指定的Web服务器运行的不是Asp.net1.1版。您将无法运行Asp.net Web应用程序或服务”问题的解决方法