tensorFlow七:tensorflow的运行设备

import tensorflow as tf
import os
# 屏蔽info
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'

with tf.device("/cpu:0"):
# with tf.device("/gpu:0"):
# with tf.device("/gpu:1"):
    a = tf.Variable(initial_value=[2, 3, 4], dtype=tf.int32, name='a')
    b = tf.constant(2, dtype=tf.int32, name='b')
    c = tf.add(a, b, name='c')
    init_op = tf.global_variables_initializer()

# allow_soft_placement=True:是否允许软切换设备,一般情况下,是必须允许的。
# 比如,当使用with tf.device("/gpu:1")指定运行设备时,如果此参数为不允许,
# 当遇到此设备不可运行的代码时,也不会切换,则会引起异常
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)) as sess:
    sess.run(init_op)
    print("result>>> {}".format(sess.run(c)))

 

posted on 2019-02-01 20:53  myworldworld  阅读(125)  评论(0)    收藏  举报

导航