tensorflow中使用tf.ConfigProto()使用方法

tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置:

config = tf.ConfigProto(allow_soft_placement=True, allow_soft_placement=True)
config.gpu_options.per_process_gpu_memory_fraction = 0.4  #占用40%显存
sess = tf.Session(config=config)

 

1. 记录设备指派情况 :  tf.ConfigProto(log_device_placement=True)

他可以用在

sess = tf.Session(config=tf.ConfigProto(
      allow_soft_placement=True, log_device_placement=True))
或者
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
    .....

或者像上面的例子那样。

他的作用是显示值/运算所储存/使用的设备信息,CPU还是GPU。


2. 自动选择运行设备 : tf.ConfigProto(allow_soft_placement=True)

在用"with tf.device('/cpu:0'):"指定使用设备时候,可能这个设备被占用,会出错。当出错时候,这个命会自己修改使用的设备。


with tf.device('/gpu:2'):
  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
  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)
# 新建 session with log_device_placement 并设置为 True.
sess = tf.Session(config=tf.ConfigProto(
      allow_soft_placement=True, log_device_placement=True))
# allow_soft_placement=True选择可用的设备

# 运行这个 op.
print(sess.run(c))


3. 限制GPU资源使用:

 

为了加快运行效率,TensorFlow在初始化时会尝试分配所有可用的GPU显存资源给自己,这在多人使用的服务器上工作就会导致GPU占用,别人无法使用GPU工作的情况。

tf提供了两种控制GPU资源使用的方法,一是让TensorFlow在运行过程中动态申请显存,需要多少就申请多少;第二种方式就是限制GPU的使用率。

一、动态申请显存

这里可以看我的另一个资料,https://mp.csdn.net/postedit/89979442

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    session = tf.Session(config=config)

二、限制GPU使用率

 

    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.4  #占用40%显存
    session = tf.Session(config=config)
或者:

    gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.4)
    config=tf.ConfigProto(gpu_options=gpu_options)
    session = tf.Session(config=config)

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值