title: Tensorflow 2: how to switch execution from GPU to CPU and back?
body: <p>In <code>tensorflow</code> 1.X with standalone <code>keras</code> 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:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>This <code>ConfigProto</code> functionality is no longer available in <code>tensorflow</code> 2.0 (there I'm using the integrated <code>tensorflow.keras</code>). In the beginning, it is possible to run <code>tf.config.experimental.set_visible_devices()</code> in order to e.g. disable the GPU, but any subsequent calls to <code>set_visible_devices</code> result in <code>RuntimeError: Visible devices cannot be modified after being initialized</code>. Is there a way of re-initializing the visible devices or is there another way of switching the devices available?  </p>