程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-10(1)

【tensorflow】是否使用GPU

发布于2020-03-17 15:20     阅读(1177)     评论(0)     点赞(22)     收藏(1)


  1. import tensorflow as tf
  2. a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
  3. b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
  4. c = tf.matmul(a, b)
  5. sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))
  6. print(sess.run(c))

 

 

  1. import tensorflow as tf
  2. with tf.device('/cpu:0'):
  3. a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
  4. b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')
  5. with tf.device('/gpu:0'):
  6. c = a + b
  7. # 注意:allow_soft_placement=True表明:计算设备可自行选择,如果没有这个参数,会报错。
  8. # 因为不是所有的操作都可以被放在GPU上,如果强行将无法放在GPU上的操作指定到GPU上,将会报错。
  9. sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
  10. # sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
  11. sess.run(tf.global_variables_initializer())
  12. print(sess.run(c))
  13. #打印所有的设备
  14. from tensorflow.python.client import device_lib
  15. #print(device_lib.list_local_devices())
  16. # 查看所使用的tensorflow是GPU还是CPU版本
  17. import os
  18. from tensorflow.python.client import device_lib
  19. os.environ["TF_CPP_MIN_LOG_LEVEL"] = "99"
  20. if __name__ == "__main__":
  21. print(device_lib.list_local_devices())
 


所属网站分类: 技术文章 > 博客

作者:坚持就是胜利

链接:https://www.pythonheidong.com/blog/article/263191/9400732b55ede49015ad/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

22 0
收藏该文
已收藏

评论内容:(最多支持255个字符)