【问题标题】:Assign Torch and Tensorflow models two separate GPUs为 Torch 和 Tensorflow 模型分配两个独立的 GPU
【发布时间】:2019-02-15 19:01:24
【问题描述】:

我正在比较两个预训练模型,一个在 Tensorflow 中,一个在 Pytorch 中,在具有多个 GPU 的机器上。每个模型都适合一个 GPU。它们都加载在同一个 Python 脚本中。如何将一个 GPU 分配给 Tensorflow 模型,将另一个 GPU 分配给 Pytorch 模型?

设置 CUDA_VISIBLE_DEVICES=0,1 只会告诉两个模型这些 GPU 可用 - 我如何(我猜在 Python 中)确保 Tensorflow 使用 GPU 0 而 Pytorch 使用 GPU 1?

【问题讨论】:

    标签: tensorflow cuda pytorch torch


    【解决方案1】:

    您可以参考torch.devicehttps://pytorch.org/docs/stable/tensor_attributes.html?highlight=device#torch.torch.device

    特别做

    device=torch.device("gpu:0")
    tensor = tensor.to(device)
    

    或加载预训练模型

    device=torch.device("gpu:0")
    model = model.to(device)
    

    将张量/模型放在 gpu 0 上。

    同样,tensorflow 也有 tf.device。 https://www.tensorflow.org/api_docs/python/tf/device。这里描述了它的用法https://www.tensorflow.org/guide/using_gpu

    让 tensorflow 在 gpu:0 上加载模型,

    with tf.device("gpu:0"):
         load_model_function(model_path) 
    

    【讨论】:

    • 好的,但为此我必须更改预训练模型 - 我可以以任何方式对 tensorflow 和 torch 一般执行此操作(例如,在加载库时)?
    • 您不必更改预训练模型,我正在更新我的答案以反映这一点。
    猜你喜欢
    • 2017-04-27
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2017-11-13
    • 2018-05-15
    • 2017-11-09
    相关资源
    最近更新 更多