如何减少Tensorlfow/Keras使用的CPU数量? [英] How can I reduce the number of CPUs used by Tensorlfow/Keras?

查看:184
本文介绍了如何减少Tensorlfow/Keras使用的CPU数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tensorflow 2.0的Keras API.

I am using the Keras api of Tensorflow 2.0.

在我的Keras模型上调用fit时,它将使用所有可用标签的CPU.

When calling fit on my Keras model, it uses all availabel CPUs.

我想限制使用的CPU数量.但是,它在以前的Tensorflow版本中使用的方式不再可用:

I would like to limit the number of used CPUs. However the way it used to work in former versions of Tensorflow cannot be used anymore:

tf.keras.backend.set_session(tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(
       intra_op_parallelism_threads=2, inter_op_parallelism_threads=2)))

AttributeError:模块'tensorflow.python.keras.api._v2.keras.backend'没有属性'set_session'

AttributeError: module 'tensorflow.python.keras.api._v2.keras.backend' has no attribute 'set_session'

我该怎么办?

推荐答案

在Tensorflow 2.0中,不再有会话.在急切执行中,像这样在程序开始时直接使用config API设置并行性.

In Tensorflow 2.0, there is no session anymore. In eager execution, directly use the config API to set the parallelism at the start of the program like this.

import tensorflow as tf

tf.config.threading.set_intra_op_parallelism_threads(2)
tf.config.threading.set_inter_op_parallelism_threads(2)
with tf.device('/CPU:0'):
    model = tf.keras.models.Sequential([...

https://www.tensorflow.org/api_docs/python/tf /config/threading

这篇关于如何减少Tensorlfow/Keras使用的CPU数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆