TensorFlow:测试tensorflow,以及测试是否调用GPU

import tensorflow as tf

    # print('GPU', tf.test.is_gpu_available())

    tf.compat.v1.disable_eager_execution()

    with tf.device('/cpu:0'):
        a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
        b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')
    with tf.device('/gpu:1'):
        c = a + b

    # 注意:allow_soft_placement=True表明:计算设备可自行选择,如果没有这个参数,会报错。
    # 因为不是所有的操作都可以被放在GPU上,如果强行将无法放在GPU上的操作指定到GPU上,将会报错。
    sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(allow_soft_placement=True, log_device_placement=True))
    # sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))
    sess.run(tf.compat.v1.global_variables_initializer())
    print(sess.run(c))

结果: 

2020-12-03 22:56:11.789208: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-12-03 22:56:15.136925: 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-12-03 22:56:15.139551: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-12-03 22:56:16.076500: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce GTX 860M computeCapability: 5.0
coreClock: 1.0195GHz coreCount: 5 deviceMemorySize: 2.00GiB deviceMemoryBandwidth: 74.65GiB/s
2020-12-03 22:56:16.077063: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-12-03 22:56:16.086808: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-12-03 22:56:16.095560: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-12-03 22:56:16.098372: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-12-03 22:56:16.107580: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-12-03 22:56:16.112088: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-12-03 22:56:16.129407: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-12-03 22:56:16.129839: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
2020-12-03 22:56:18.108681: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-12-03 22:56:18.109077: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102]      0 
2020-12-03 22:56:18.109340: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 0:   N 
2020-12-03 22:56:18.110039: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1241] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1370 MB memory) -> physical GPU (device: 0, name: GeForce GTX 860M, pci bus id: 0000:01:00.0, compute capability: 5.0)
2020-12-03 22:56:18.113623: I tensorflow/core/common_runtime/direct_session.cc:358] Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 860M, pci bus id: 0000:01:00.0, compute capability: 5.0

2020-12-03 22:56:18.116629: I tensorflow/core/common_runtime/placer.cc:54] add: (AddV2): /job:localhost/replica:0/task:0/device:GPU:0
2020-12-03 22:56:18.117269: I tensorflow/core/common_runtime/placer.cc:54] init: (NoOp): /job:localhost/replica:0/task:0/device:GPU:0
2020-12-03 22:56:18.117826: I tensorflow/core/common_runtime/placer.cc:54] a: (Const): /job:localhost/replica:0/task:0/device:CPU:0
2020-12-03 22:56:18.118337: I tensorflow/core/common_runtime/placer.cc:54] b: (Const): /job:localhost/replica:0/task:0/device:CPU:0
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 860M, pci bus id: 0000:01:00.0, compute capability: 5.0
add: (AddV2): /job:localhost/replica:0/task:0/device:GPU:0
init: (NoOp): /job:localhost/replica:0/task:0/device:GPU:0
a: (Const): /job:localhost/replica:0/task:0/device:CPU:0
b: (Const): /job:localhost/replica:0/task:0/device:CPU:0
[2. 4. 6.]

 

你可能感兴趣的:(tensorflow)