Tensor flow toggle between CPU/GPU
Another option is to install the cpu version and the gpu version of tensorflow in two virtual environments, detailed instructions on how to install tensorflow in virtual environments are listed here https://www.tensorflow.org/get_started/os_setup; in this way, you can have the same code running in two terminals windows one uses CPU and the other uses GPU.
try setting tf.device to cpu:0
with tf.Session() as sess:
with tf.device("/cpu:0"):
To make GPU invisible
export CUDA_VISIBLE_DEVICES=""
To return to normal
unset CUDA_VISIBLE_DEVICES