测试版本
TensorFlow-2.4.1
TensorFlow-1.15.0
代码
# tf 2.x
############################## code #####################################
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
a = tf.constant(1.)
b = tf.constant(2.)
print(a+b)
print('GPU:', tf.config.list_physical_devices('GPU')) #gpu
print(tf.test.is_built_with_cuda()) # cuda
########################################################################
# console output:
# ...
# other logs
# ...
# tf.Tensor(3.0, shape=(), dtype=float32)
# GPU: [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
# True
# tf 1.x
############################## code #####################################
import tensorflow as tf
sess = tf.Session()
a = tf.constant([1,2,3],shape=[3],name='a')
b = tf.constant([1,2,3],shape=[3],name='b')
c=a+b
print(sess.run(c))
print(tf.test.is_gpu_available()) # gpu
print(tf.test.is_built_with_cuda()) # cuda
########################################################################
# console output:
# ...
# other logs
# ...
# [2 4 6]
# ...
# other logs
# ...
# True
# True