仅在 CPU 上运行 TensorFlow Graph - 使用 tf.config

import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(device_count={'GPU': 0}))

请记住,此方法会阻止 TensorFlow 图使用 GPU,但 TensorFlow 仍会锁定 GPU 设备,如此方法中打开的问题所述。使用 CUDA_VISIBLE_DEVICES 似乎是确保 TensorFlow 远离 GPU 卡的最佳方法(请参阅此答案 )。