Tutorial--怎么看自己安装的Tensorflow是GPU版本的还是CPU版本的
2019-06-29 20:33
3219 查看
例子1:
为了获取你的 operations 和 Tensor 被指派到哪个设备上运行, 用log_device_placement 新建一个 session, 并设置为 True来记录设备指派情况。
例子(矩阵相乘):
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)) #任务完成,关闭会话 sess.close()
结果:
显示的是CPU版本的Tensorflow。
例子2:
在自己安装好的python环境下输入以下代码:
import numpy import tensorflow as tf 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) sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) print(sess.run(c))
结果会出现详细的信息:
可以看到我所安装的Tensorflow是CPU在工作。
例子3:
第一步:
在python环境下输入以下代码:
import os from tensorflow.python.client import device_lib os.environ["TF_CPP_MIN_LOG_LEVEL"] = "99" if __name__ == "__main__": print(device_lib.list_local_devices())
结果(查看电脑GPU和CPU):
我自己的电脑只有CPU,没有GPU。所以只能看到关于电脑CPU的信息。如果电脑GPU和CPU都有的话应该是如下结果:
第二步:
指定电脑GPU或者CPU进行计算,代码如下:
#使用CPU进行计算 with tf.device("/cpu:0"): 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 = tf.matmul(a,b) #查看计算时硬件的使用情况 sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) print(sess.run(c))
使用代码结果如下:
通过tf.device可以指定计算时使用的设备,0表示设备的个数。如果想要使用GPU进行计算,就将CPU改成GPU。
第三步:
查看Tensorflow的详细情况,代码如下:
#设置运行时候的参数 options = tf.RunOptions(output_partition_graphs=True) metadata = tf.RunMetadata() c_val = sess.run(c,options=options,run_metadata=metadata) print(metadata.partition_graphs) #关闭session sess.close()
明天试试,电脑能行吗=.=
相关文章推荐
- 确定自己的TensorFlow是CPU还是GPU的版本
- TensorFlow - win7 - CPU/GPU版本安装
- TensorFlow安装方法三【Anaconda方式下查看TensorFlow版本选择性安装】(Windows10 64位 cpu and gpu)
- 如何在Linux 系统 以及windows 系统安装 tensorflow -cpu -gpu 老版本 0.12 ,0.10
- ubuntu下安装gpu版本tensorflow
- ubuntu16.04LTS安装tensorflow1.1.0(pip方式/python2.7版本/cpu版本)
- 64位WIN10通过anaconda安装CPU版本tensorflow
- Windows下Anaconda、tensorflow、pycharm的安装(cpu版本)
- Ubuntu安装Tensorflow GPU 版本和 CUDA Toolkit 9.1 和 cuDNN 7.0.5 for Python 3
- 记录:Ubuntu 18.04 安装 tensorflow-gpu 版本
- ubuntu16.04下使用anaconda安装tensorflow_gpu版本以及object detection的过程
- tensorflow windows CPU 版本的配置+cv2安装
- 六行代码安装 GPU版本的TensorFlow
- tensorflow cpu版本安装
- Ubuntu16.04LTS安装TensorFlow 1.1.0 GPU版本(极速版)
- tensorflow_gpu-1.2.1-cp35-cp35m-win_amd64.whl文件怎么安装?
- Ubuntu下Tensorflow cpu版本安装介绍
- Win10下安装TensorFlow(GPU版本)
- Windows10安装TensorFlow-GPU版本