Tensorflow选择GPU&CPU计算与训练==1

29 篇文章 2 订阅
5 篇文章 0 订阅
结果分析:Tensorflow搭载GPU与CPU计算与训练==2

1 默认模式

1.0 默认CPU

  • 环境
  • Ubuntu16.04
  • python2.7
  • Tensorflow1.12.0
  • 源码Demo
v1 = tf.constant([125.0], shape=[1], name='v1')
v2 = tf.constant([125.0], shape=[1], name='v2')
result = v1 + v2
# 输出使用设备的信息True
config = tf.ConfigProto(log_device_placement=True)
with tf.Session(config=config) as sess:
	print("GPU calculator result is : {}".format(sess.run(result)))
	print("CPU calculator time consuming is: {}".format(timeConsuming))

1.2 默认GPU

  • 环境
  • Ubuntu16.04
  • python2.7
  • Tensorflow-gpu 1.4.0
  • CUDA 8
  • cuDNN 6
  • 源码Demo
v1 = tf.constant([125.0], shape=[1], name='v1')
v2 = tf.constant([125.0], shape=[1], name='v2')
result = v1 + v2
# 输出使用设备的信息True
config = tf.ConfigProto(log_device_placement=True)
with tf.Session(config=config) as sess:
	print("GPU calculator result is : {}".format(sess.run(result)))
	print("GPU calculator time consuming is: {}".format(timeConsuming))

2 指定模式

2.1 全局指定

2.1.0 全局CPU

  • 环境
  • Ubuntu16.04
  • python2.7
  • Tensorflow1.12.0
  • 源码Demo
import os 
# 定义放在导入tensorflow前
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
import tensorflow as tf
# CPU default
v1 = tf.constant([125.0], shape=[1], name='v1')
v2 = tf.constant([125.0], shape=[1], name='v2')
startTime = time.time()
result = v1 + v2
endTime = time.time()
timeConsuming = endTime - startTime
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
	print("CPU calculator result is : {}".format(sess.run(result)))
	print("CPU calculator time consuming is: {}".format(timeConsuming))

2.1.2 全局GPU

  • 环境
  • Ubuntu16.04
  • python2.7
  • Tensorflow-gpu 1.4.0
  • Tensorflow 1.12.0
  • CUDA 8
  • cuDNN 6
  • 源码Demo
import os
# 全局指定GPU编号
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import tensorflow as tf
# GPU default
v1 = tf.constant([125.0], shape=[1], name='v1')
v2 = tf.constant([125.0], shape=[1], name='v2')
startTime = time.time()
result = v1 + v2
endTime = time.time()
timeConsuming = endTime - startTime
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
	print("GPU calculator result is : {}".format(sess.run(result)))
	print("GPU calculator time consuming is: {}".format(timeConsuming))

2.2 局部指定

2.2.1 局部CPU

  • 环境
  • Ubuntu16.04
  • python2.7
  • Tensorflow-gpu 1.4.0
  • Tensorflow 1.12.0
  • CUDA 8
  • cuDNN 6
  • 源码Demo
with tf.device('/cpu:0'):
	v1 = tf.constant([125.0], shape=[1], name='v1')
	v2 = tf.constant([125.0], shape=[1], name='v2')
	result = v1 + v2
	# 输出使用设备的信息True
	config = tf.ConfigProto(log_device_placement=True)
	with tf.Session(config=config) as sess:
		print("CPU calculator result is : {}".format(sess.run(result)))
       print("CPU calculator time consuming is: {}".format(timeConsuming))

2.2.2 局部GPU

  • 环境
  • Ubuntu16.04
  • python2.7
  • Tensorflow-gpu 1.4.0
  • Tensorflow 1.12.0
  • CUDA 8
  • cuDNN 6
  • 源码Demo
with tf.device('/gpu:0'):
	v1 = tf.constant([125.0], shape=[1], name='v1')
	v2 = tf.constant([125.0], shape=[1], name='v2')
	result = v1 + v2
	# 输出使用设备的信息True
	config = tf.ConfigProto(log_device_placement=True)
	with tf.Session(config=config) as sess:
		print("GPU calculator result is : {}".format(sess.run(result)))
       print("GPU calculator time consuming is: {}".format(timeConsuming))

3 总结

  • 依据不同需求,选择不同环境;
  • GPU与CPU可全局配置也可局部配置,多GPU可选择局部配置;
  • CPU与GPU计算速度不同(废话),单纯CPU环境与GPU环境下的CPU处理效率是不同的,参见另一篇文章.
  • Tensorflow选择GPU&CPU计算与训练==2

[参考文献]
[1]https://blog.csdn.net/qq_35559420/article/details/81460912
[2]https://blog.csdn.net/dcrmg/article/details/79747882


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值