TensorFlow之CPU、GPU设置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zong596568821xp/article/details/83302344

1 指定GPU

法1 

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "1"

上边表示使用第二块GPU运行程序,如果要使用多块,如第一块和第三块,可使用如下方法指定

os.environ["CUDA_VISIBLE_DEVICES"] = "0,2"

法2

import tensorflow as tf
 
with tf.device('/gpu:1'):
    v1 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v1')
    v2 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v2')
    sumV12 = v1 + v2
 
    with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
        print sess.run(sumV12)

2 指定CPU

cpu_num=10#指定使用的CPU个数
config = tf.ConfigProto(device_count={"CPU": cpu_num},
            inter_op_parallelism_threads = cpu_num,
            intra_op_parallelism_threads = cpu_num,
            log_device_placement=True)
# 开始训练
with tf.Session(config=config) as sess:
    #以下编写自己的代码

log_device_placement = True可以把程序运行时跑的设备情况给输出

猜你喜欢

转载自blog.csdn.net/zong596568821xp/article/details/83302344