使用方法:tf.device(‘/cpu:0’)或tf.device(‘/gpu:0’)。
实例:
import tensorflow as tf
with tf.device('/cpu:0'):
a = tf.constant([1.,2.,3.],shape=[3],name='a')
b = tf.constant([2.,3.,4.],shape=[3],name='b')
with tf.device('/gpu:0'):
c = a + b
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(c)
会打印出: