import tensorflow as tf
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.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))# sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(tf.global_variables_initializer())print(sess.run(c))
tensorflow 2.0及以上版本GPU测试
import tensorflow as tf
#查看tensorflow版本print(tf.__version__)print('GPU', tf.test.is_gpu_available())
a = tf.constant(2.0)
b = tf.constant(4.0)print(a + b)