Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reduce the number of CPUs used by Tensorlfow/Keras?

I am using the Keras api of Tensorflow 2.0.

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

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: module 'tensorflow.python.keras.api._v2.keras.backend' has no attribute 'set_session'

How could I do that?

like image 353
Pop Avatar asked Sep 13 '19 13:09

Pop


People also ask

Does TensorFlow automatically use multiple CPUs?

All cores are wrapped in cpu:0, i.e., TensorFlow does indeed use multiple CPU cores by default.

Does keras use CPU or GPU?

Using Your GPU For Model Training With KerasIf a TensorFlow operation has both CPU and GPU implementations, by default the GPU will be used by default.

Can TensorFlow GPU run on CPU?

TensorFlow supports running computations on a variety of types of devices, including CPU and GPU.

Can keras run on CPU?

Via TensorFlow (or Theano, or CNTK), Keras is able to run seamlessly on both CPUs and GPUs.


1 Answers

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

like image 76
Manoj Mohan Avatar answered Oct 10 '22 04:10

Manoj Mohan