在gpu上训练的模型可以在cpu上用于推理吗?

时间:2020-07-10 23:33:51

标签: tensorflow tensorflow-serving

尽管我的机器有GPU,但我想推断我的模型使用CPU。 想知道是否可以强制模型使用CPU而不是GPU?

默认情况下,模型会自动使用GPU进行推理,但是由于我的GPU不好(OOM),想知道Tensorflow中是否存在可以强制使用CPU进行推理的配置?

我使用的推理代码:

if (_data.Item is Item item)
{ 
    // use item
}

1 个答案:

答案 0 :(得分:0)

假设您正在使用TensorFlow 2.0,请在GitHub上查看此问题:

解决方案似乎是在TensorFlow中隐藏GPU设备。您可以使用下面介绍的一种方法来做到这一点:

TensorFlow 2.0:

my_devices = tf.config.experimental.list_physical_devices(device_type='CPU')
tf.config.experimental.set_visible_devices(devices= my_devices, device_type='CPU')

TensorFlow 2.1:

tf.config.set_visible_devices([], 'GPU')

(感谢@ymodak和@henrysky,他们回答了GitHub问题。)

相关问题