参考Tensorflow Machine Leanrning Cookbook

tf.ConfigProto()主要的作用是配置tf.Session的运算方式,比如gpu运算或者cpu运算

具体代码如下:

import tensorflow as tf

session_config = tf.ConfigProto(
log_device_placement=True,
inter_op_parallelism_threads=0,
intra_op_parallelism_threads=0,
allow_soft_placement=True)

sess = tf.Session(config=session_config)

a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2,3], name='b')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3,2], name='b')

c = tf.matmul(a,b)
print(sess.run(c))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

具体解释

log_device_placement=True

设置为True时,会打印出TensorFlow使用了那种操作

inter_op_parallelism_threads=0

设置线程一个操作内部并行运算的线程数,比如矩阵乘法,如果设置为0,则表示以最优的线程数处理

intra_op_parallelism_threads=0

设置多个操作并行运算的线程数,比如 c = a + b,d = e + f . 可以并行运算

allow_soft_placement=True

有时候,不同的设备,它的cpu和gpu是不同的,如果将这个选项设置成True,那么当运行设备不满足要求时,会自动分配GPU或者CPU。

其他选项

当使用GPU时候,Tensorflow运行自动慢慢达到最大GPU的内存

session_config.gpu_options.allow_growth = True
1

当使用GPU时,设置GPU内存使用最大比例

session_config.gpu_options.per_process_gpu_memory_fraction = 0.4
1

是否能够使用GPU进行运算

tf.test.is_built_with_cuda()
1

另外的处理方法

import tensorflow as tf

sess = tf.Session()

with tf.device('/cpu:0'):
a = tf.constant([1.0, 3.0, 5.0], shape=[1, 3])
b = tf.constant([2.0, 4.0, 6.0], shape=[3, 1])

with tf.device('/gpu:0'):
c = tf.matmul(a, b)
c = tf.reshape(c, [-1])

with tf.device('/gpu:0'):
d = tf.matmul(b, a)
flat_d = tf.reshape(d, [-1])

combined = tf.multiply(c, flat_d)
print(sess.run(combined))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

---------------------
作者:泥石流中的一股清流
来源:CSDN
原文:https://blog.csdn.net/qq_31261509/article/details/79746114
版权声明:本文为博主原创文章,转载请附上博文链接!

最新文章

  1. matlab备忘录
  2. SpringIOC使用扩展
  3. Python 练习册
  4. nginx location语法使用说明
  5. django的分页--不全也未实现
  6. SharePoint 2010 BCS - 简单实例(一)数据源添加
  7. UVALive 7455 Linear Ecosystem (高斯消元)
  8. 【转载】Android设计中的.9.png
  9. Gym 100187M-Heaviside Function
  10. 学习总结之Log4NET
  11. String功能测试
  12. 并行开发学习随笔1——plinq并行
  13. jQuery 之 $(this) 出了什么问题?
  14. BAT级别分类
  15. Android拼图游戏的设计逻辑,从切图到交互动画,从关卡到倒计时,实例提高!
  16. gradle 将依赖打入Jar包的方法
  17. ionic1 添加百度地图插件 cordova-plugin-baidumaplocation
  18. Jquery+asp.net实现Ajax方式文件下载实例代码
  19. SQL Server-聚焦深入理解死锁以及避免死锁建议(转载)
  20. 在AJAX里 使用【 XML 】 返回数据类型 实现简单的下拉菜单数据

热门文章

  1. 洛谷P1080 [NOIP2012提高组D1T2]国王游戏 [2017年5月计划 清北学堂51精英班Day1]
  2. sending data mysql slow Mysql查询非常慢的可能原因
  3. Winform实现调用asp.net数据接口实例
  4. 三分钟学会在ASP.NET Core MVC 中使用Cookie
  5. 洛谷P1929 迷之阶梯
  6. vue2-vux-fitness-project
  7. 新一代视频AI服务 —— 阿里云智能视觉重磅发布
  8. centos6.5后台进程的切换
  9. Python之路,Day1 - Python基础1 --转自金角大王
  10. python 终端编码