In tensorflow
1.X with standalone keras
2.X, I used to switch between training on GPU, and running inference on CPU (much faster for some reason for my RNN models) with the following snippet:
keras.backend.clear_session()
def set_session(gpus: int = 0):
num_cores = cpu_count()
config = tf.ConfigProto(
intra_op_parallelism_threads=num_cores,
inter_op_parallelism_threads=num_cores,
allow_soft_placement=True,
device_count={"CPU": 1, "GPU": gpus},
)
session = tf.Session(config=config)
k.set_session(session)
This ConfigProto
functionality is no longer available in tensorflow
2.0 (there I'm using the integrated tensorflow.keras
). In the beginning, it is possible to run tf.config.experimental.set_visible_devices()
in order to e.g. disable the GPU, but any subsequent calls to set_visible_devices
result in RuntimeError: Visible devices cannot be modified after being initialized
. Is there a way of re-initializing the visible devices or is there another way of switching the devices available?