Perfect solution to check whether the tensorflow you are using is cpu or gpu version

References: Perfect solution to the error in executing sess =Session () under tensorflow 2.1.0

method one:

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

insert image description here

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
    
    
}
incarnation: 3236465611047001069
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 1451743641
locality {
    
    
  bus_id: 1
  links {
    
    
  }
}
incarnation: 8073041557362709337
physical_device_desc: "device: 0, name: GeForce MX130, pci bus id: 0000:02:00.0, compute capability: 5.0"
]

进程已结束,退出代码0

Method Two:

Enter python, tensorflow environment
Mac OS terminal, type:
python
import tensorflow as tf
tf.__version__ (note: it is two underscores), this command checks the version of tensorflow currently installed
tf. __ path__ (note: it is two underscores), this command checks the installation directory of currently installed tensorflow

import tensorflow as tf
print(tf.__version__)

Complete test code:

# -!- coding: utf-8 -!-
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
hello = tf.constant('hello,tensorflow')
sess= tf.compat.v1.Session()
print(sess.run(hello))
# from tensorflow.python.client import device_lib
# print(device_lib.list_local_devices())

print(tf.__version__)

result:

b'hello,tensorflow'
2.0.0

进程已结束,退出代码0

Method three:

Remove: os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' to display the word GPU, and use the code generation directly as follows: mainly add in the session: config=tf.compat.v1.ConfigProto(log_device_placement=True) to print out whether it is CPU or GPU.

import tensorflow as tf
tf.compat.v1.disable_eager_execution()#保证sess.run()能够正常运行
hello = tf.constant('hello,tensorflow')
sess= tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))#版本2.0的函数
print(sess.run(hello))

The execution result shows Your CPU supports instructions, indicating that it is CPU, otherwise it is GPU

2020-11-15 12:16:24.624807: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-11-15 12:16:24.627359: I tensorflow/core/common_runtime/direct_session.cc:359] Device mapping:

b'hello,tensorflow'
2020-11-15 12:16:24.650728: I tensorflow/core/common_runtime/placer.cc:54] Const: (Const): /job:localhost/replica:0/task:0/device:CPU:0

Process finished with exit code 0

Or count a few examples:

import numpy
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()#保证sess.run()能够正常运行
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))

Results of the:

Device mapping: no known devices.
MatMul: (MatMul): /job:localhost/replica:0/task:0/device:CPU:0
a: (Const): /job:localhost/replica:0/task:0/device:CPU:0
b: (Const): /job:localhost/replica:0/task:0/device:CPU:0
[[22. 28.]
 [49. 64.]]

Process finished with exit code 0

or:

Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce MX130, pci bus id: 0000:02:00.0, compute capability: 5.0
MatMul: (MatMul): /job:localhost/replica:0/task:0/device:GPU:0
a: (Const): /job:localhost/replica:0/task:0/device:GPU:0
b: (Const): /job:localhost/replica:0/task:0/device:GPU:0
[[22. 28.]
 [49. 64.]]

进程已结束,退出代码0

Guess you like

Origin blog.csdn.net/weixin_41194129/article/details/107763236#comments_26847457
Recommended