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?
All cores are wrapped in cpu:0, i.e., TensorFlow does indeed use multiple CPU cores by default.
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.
TensorFlow supports running computations on a variety of types of devices, including CPU and GPU.
Via TensorFlow (or Theano, or CNTK), Keras is able to run seamlessly on both CPUs and GPUs.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With