Ubuntu16.04测试开启GPU加速

来源:互联网 发布:旋转矩阵计算旋转角度 编辑:程序博客网 时间:2024/06/10 04:20

在前面文章中《Ubuntu16.04+GTX1050ti+CUDA8.0+TensorFlow-gpu+keras配置深度学习环境》已经配置好了系统。现在来进行GPU加速测试。

  • 测试1:系统自动分配设备
#-*- coding:utf-8 -*-import tensorflow as tf# 新建一个 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 busid: 0000:05:00.0b: /job:localhost/replica:0/task:0/gpu:0a: /job:localhost/replica:0/task:0/gpu:0MatMul: /job:localhost/replica:0/task:0/gpu:0[[ 22.  28.] [ 49.  64.]]
  • 测试2:手动指定分配设备
#-*- coding:utf-8 -*-import tensorflow as tf# 新建一个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 busid: 0000:05:00.0b: /job:localhost/replica:0/task:0/cpu:0a: /job:localhost/replica:0/task:0/cpu:0MatMul: /job:localhost/replica:0/task:0/gpu:0[[ 22.  28.] [ 49.  64.]]
  • 测试3:跑个TensorFlow的小程序。代码见百度云盘

参考文献:

  1. Using GPUs
  2. 使用 GPUs
阅读全文
0 0
原创粉丝点击