with g.device("/gpu:0"):
def device_for_node(n):
if n.type == "MatMul":
return "/gpu:0"
else:
return "/cpu:0"
with g.as_default():
with g.device(device_for_node):
如果 GPU 不可用,注意使用 allow_soft_placement 返回到 CPU。如果你编码的东西无法在 GPU 运行,那么可以移动到 CPU 中:
with tf.device("/device:GPU:0"): a = tf.placeholder(tf.uint8, shape=(4)) b = a[..., -1] sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) sess.run(tf.global_variables_initializer()) # Seems to work fine. But with allow_soft_placement=False sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=False)) sess.run(tf.global_variables_initializer()) # we get # Cannot assign a device for operation 'strided_slice_5': # Could not satisfy explicit device specification '/device:GPU:0' # because no supported kernel for GPU devices is available.
GPU其他相关: