前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tf.ConfigProto类

tf.ConfigProto类

作者头像
狼啸风云
修改2022-09-04 21:35:45
1.3K0
修改2022-09-04 21:35:45
举报

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

allow_soft_placement

  • bool allow_soft_placement

cluster_def

  • ClusterDef cluster_def

device_count

  • repeated DeviceCountEntry device_count

device_filters

  • repeated string device_filters

experimental

  • Experimental experimental

gpu_options

  • GPUOptions gpu_options

graph_options

  • GraphOptions graph_options

inter_op_parallelism_threads

  • int32 inter_op_parallelism_threads

intra_op_parallelism_threads

  • int32 intra_op_parallelism_threads

isolate_session_state

  • bool isolate_session_state

log_device_placement

  • bool log_device_placement

operation_timeout_in_ms

  • int64 operation_timeout_in_ms

placement_period

  • int32 placement_period

rpc_options

  • RPCOptions rpc_options

session_inter_op_thread_pool

  • repeated ThreadPoolOptionProto session_inter_op_thread_pool

use_per_session_threads

  • bool use_per_session_threads

例:

代码语言:javascript
复制
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的内存

代码语言:javascript
复制
session_config.gpu_options.allow_growth = True

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

代码语言:javascript
复制
session_config.gpu_options.per_process_gpu_memory_fraction = 0.4

是否能够使用GPU进行运算

代码语言:javascript
复制
tf.test.is_built_with_cuda()

另外的处理方法

代码语言:javascript
复制
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))
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年07月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档