设置GPU个数
设置单个GPU或者CPU
如果没有事先指定GPU或者CPU,那么TensorFlow默认的机制是寻找在机器中找到的第一个GPU,并且占有该GPU所有的显存资源。如果没有找到GPU那么默认是利用第一个CPU来做运算。首先说一下TensorFlow对于设备的表示:”/cpu:0”表示第一块CPU,”/gpu:0”表示第一块GPU,其他情况以此类推。我们可以用nvidia-smi来查看设备GPU信息。如下图所示:
我们就用TensorFlow官网的例子作为示例:
1
2
3
4
5
6
7
8
9
# Creates a graph.
with
tf.device('/gpu:2'):
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)
# Creates a session with
log_device_placement set to True.
sess =
tf.Session(config=tf.ConfigProto(log_device_