Tensorflow细节-P319-使用GPU基本的操作
2024-09-03 18:54:40
如果什么都不加,直接运行装了GPU的Tensorflow,结果是这样子的
import tensorflow as tf
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
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) # 通过log_device_placement参数来记录运行每一个运算的设备。
print(sess.run(c))
来来来,看图
当指定cpu还是GPU时:
import tensorflow as tf
# 通过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:0'):
c = a + b
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess.run(c))
结果就变成了这样子:
最后一个
import tensorflow as tf
a_cpu = tf.Variable(0, name="a_cpu")
with tf.device('/gpu:0'):
a_gpu = tf.Variable(0, name="a_gpu")
# 通过allow_soft_placement参数自动将无法放在GPU上的操作放回CPU上。
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
sess.run(tf.global_variables_initializer())
结果:
最新文章
- 从零自学Hadoop(10):Hadoop1.x与Hadoop2.x
- EasyUI、Struts2、Hibernate、spring 框架整合
- javascript面向对象(学习和理解)
- SQL Server case表达式的用法
- CSS基础(一):开篇
- watchdog机制
- HDU 5285 wyh2000 and pupil
- 从服务器将Oracle数据库导出到本地Oracle数据库的方法
- css样式图片、渐变、相关小知识
- <;转载>;构造函数声明为Private和Protected
- [妙味DOM]第六课:鼠标滚轮和COOKIE
- Ubuntu系统怎么切换多用户命令界面
- IoT experitment
- linux resin 基本站点配置
- RAC手动中断订阅
- a标签的四个伪类
- CentOS 7不能联网解决办法
- iOS最为简单时间轴(GZTimeLine)
- [转]乔布斯的薄伽梵歌 Steve&rsquo;s Bhagavat Gita
- mac iterm2
热门文章
- Jenkins服务使用 宿主机的docker、docker-compose (Jenkins 执行sudo命令时出现“sudo: no tty present and no askpass program specified”,以及 docker-compose command not found解决办法)
- PHP的序列化、对象、反射、异常与错误
- Outlook 邮件助手
- dump net core lldb 安装
- 数组中[::-1]或[::-n]的区别,如三维数组[:,::-1,:]
- SP375 QTREE - Query on a tree (树剖)
- pandas-01 Series()的几种创建方法
- git拉取单个子目录
- 关于创建Web图像时应记住的五个要素
- 使用SAP Cloud Application Programming模型开发OData的一个实际例子