I’ve got Keras set up with TensorFlow backend and CUDA installed. Sometimes I want to make Keras use the CPU instead of the GPU. Is there a way to do this without setting up a separate CPU-only TensorFlow in a virtual environment?
I know that if I was using Theano as the backend, I could use flags to control this. But I’m not sure if there are similar options for TensorFlow that I can access through Keras.
Does anyone know if it’s possible to switch between CPU and GPU on the fly? And if so, how can I do it? I’d really appreciate any tips or suggestions on how to manage this. Thanks!
ooh, that’s interesting! have u tried using tf.config.set_visible_devices()? i heard it’s more flexible. can u switch mid-script? and does it affect performance? i’m curious how this compares to using separate environments. anyone experimented with both approaches?
I’ve found that using tf.config.set_visible_devices() offers more granular control over device selection. It allows you to specify which GPUs to use or disable them entirely:
tf.config.set_visible_devices([], 'GPU') # Disable all GPUs
tf.config.set_visible_devices([tf.config.list_physical_devices('GPU')[0]], 'GPU') # Use first GPU
This method can be called at any point in your script, enabling dynamic switching between devices. However, it’s crucial to call it before any TensorFlow operations are executed. In my experience, the performance impact is minimal when switching, but you may notice a slight delay as TensorFlow reinitializes the computational graph on the new device.