tf.device() 查看和指定 GPU或CPU 设备

GPU_device.py:

# https://blog.csdn.net/lemon_tree1994/article/details/79206790
# https://blog.csdn.net/dcrmg/article/details/79747882
import tensorflow as tf
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"   #指定gpu
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))   #打印gpu信息
# os.system("pause")

def examine():
    # TensorFlow可以通过设置 log_device_placement 参数来打印运行每一个运算的设备。
    a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
    b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')
    c = a + b
    # 通过log_device_placement参数来输出运行每一个运算的设备。
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
    print(sess.run(c))

def allocate():
    # TensorFlow会自动优先将运算放置在GPU上
    # 通过tf.device将运算指定到特定的设备上
    # 如果需要将某些运算放到不同的GPU或者CPU上,就需要通过tf.device来手工指定
    with tf.device('/cpu:0'):
       a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
       b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')
    with tf.device('/gpu:1'):
        c = a + b

    sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
    print(sess.run(c))

if __name__=='__main__':
    examine()
    # allocate()

 

或者在pycharm 中使用 CUDA_VISIBLE_DEVICES:

参考资料: https://www.cnblogs.com/darkknightzh/p/8576825.html

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值