Tensorflow中tf.ConfigProto()的用法详解
作者:泥石流中的一股清流 时间:2022-01-12 03:33:25
参考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))
具体解释
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
当使用GPU时,设置GPU内存使用最大比例
session_config.gpu_options.per_process_gpu_memory_fraction = 0.4
是否能够使用GPU进行运算
tf.test.is_built_with_cuda()
另外的处理方法
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))
来源:https://blog.csdn.net/qq_31261509/article/details/79746114
标签:Tensorflow,tf.ConfigProto
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python如何通过变量ID得到变量的值
2023-01-22 22:35:56
![](https://img.aspxhome.com/file/2023/4/65884_0s.jpg)
Python pandas 重命名索引和列名称的实现
2023-11-10 13:31:04
![](https://img.aspxhome.com/file/2023/3/64363_0s.png)
在asp中使用存储过程
2008-02-26 12:17:00
Python实现的各种常见分布算法示例
2021-12-06 18:26:15
![](https://img.aspxhome.com/file/2023/1/76061_0s.png)
JavaScript调试之console.log调试的一个小技巧分享
2023-06-28 17:23:49
![](https://img.aspxhome.com/file/2023/3/55903_0s.png)
Python的flask常用函数route()
2023-03-10 09:54:39
JS简单实现DIV相对于浏览器固定位置不变的方法
2023-08-05 22:30:12
asp 延时 页面延迟的三种方法
2011-03-31 11:04:00
Python中格式化字符串输出的4种方式小结
2023-08-10 21:30:49
通过屏蔽IP来防止采集
2007-08-19 15:28:00
php中实现记住密码自动登录的代码
2023-11-14 18:36:14
让设计散发文化韵味
2009-03-22 15:01:00
![](https://img.aspxhome.com/file/UploadPic/20093/22/e69687e58c96e6a8a1e59e8b-85s.jpg)
在XPath查询中指定轴(转自MSSQL手册)
2008-09-04 14:23:00
python通过http上传文件思路详解
2022-02-08 12:48:30
![](https://img.aspxhome.com/file/2023/9/79419_0s.png)
浅析facebook的信息架构
2008-07-25 19:57:00
![](https://img.aspxhome.com/file/UploadPic/20087/25/200872520818926s.png)
Python爬虫爬取微博热搜保存为 Markdown 文件的源码
2023-03-03 23:30:29
![](https://img.aspxhome.com/file/2023/7/67297_0s.jpg)
关于字符集和Unicode的相关知识[译]
2010-03-13 14:17:00
![](https://img.aspxhome.com/file/UploadPic/20103/13/01-36s.png)
python 利用turtle库绘制笑脸和哭脸的例子
2022-01-16 08:35:30
![](https://img.aspxhome.com/file/2023/7/86157_0s.jpg)
关于Python面向对象编程的知识点总结
2021-06-21 18:15:43
Python处理字符串的常用函数实例总结
2022-10-19 21:09:39
![](https://img.aspxhome.com/file/2023/5/75175_0s.png)