僅在 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 卡的最佳方法(請參閱此答案 )。