【问题标题】:For tensorflow 2.x, how to switch between the CPU and GPU version?tensorflow 2.x,CPU和GPU版本如何切换?
【发布时间】:2020-06-09 09:19:01
【问题描述】:
我已将 anaconda 更新到最新版本。它同时安装了 tensorflow 2.2 和 tensorflow-gpu 2.2。但是当我import tensorflow 时,tensorflow-gpu 是默认使用的。有没有办法在它们之间切换?
这个网站上问的类似问题都是针对1.x版本的。我已经尝试了这些解决方案,但它似乎不适用于 2.x 版本。 Tensor flow toggle between CPU/GPU
【问题讨论】:
标签:
tensorflow
anaconda
tensorflow2.0
【解决方案1】:
这应该可以解决您的问题:
with tf.device('/gpu:0'):
# YOUR def main() OR model.fit()
with tf.device('/cpu:0'):
# YOUR def main() OR model.fit()
这应该适用于没有会话的 TF2。
【解决方案2】:
另一种方法是:
#use gpu 0
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.set_visible_devices(physical_devices[0], 'GPU')
#use cpu
tf.config.set_visible_devices([], 'GPU')