【问题标题】:how to run two different neural network on two GPUs?如何在两个 GPU 上运行两个不同的神经网络?
【发布时间】:2020-03-06 22:20:59
【问题描述】:

我有两个 GPU,但现在只有一个用于模型训练,那么如何在另一个 GPU 上运行另一个神经网络?

【问题讨论】:

    标签: python tensorflow gpu


    【解决方案1】:

    如果您使用的是 pytorch,并且安装了 CUDA,则可以使用他们的 .to() 函数

    pytorch 文档上的一个简单示例显示:

    >>> tensor = torch.randn(2, 2)  # Initially dtype=float32, device=cpu
    >>> tensor.to(torch.float64)
    tensor([[-0.5044,  0.0005],
            [ 0.3310, -0.0584]], dtype=torch.float64)
    
    >>> cuda0 = torch.device('cuda:0')
    >>> tensor.to(cuda0)
    tensor([[-0.5044,  0.0005],
            [ 0.3310, -0.0584]], device='cuda:0')
    
    >>> tensor.to(cuda0, dtype=torch.float64)
    tensor([[-0.5044,  0.0005],
            [ 0.3310, -0.0584]], dtype=torch.float64, device='cuda:0')
    
    >>> other = torch.randn((), dtype=torch.float64, device=cuda0)
    >>> tensor.to(other, non_blocking=True)
    tensor([[-0.5044,  0.0005],
            [ 0.3310, -0.0584]], dtype=torch.float64, device='cuda:0')
    

    您可以在此处参考这些链接:

    https://pytorch.org/docs/stable/tensors.html#torch.Tensor.to https://pytorch.org/docs/stable/cuda.html#torch.cuda.device

    【讨论】:

    • 所以只需要我在我想使用的gpu上分配任务?
    • 是的,这是一种方法。我忘记提到的另一种方法是您可以使用CUDA_VISIBLE_DEVICES 方法,尽管我只在 linux 系统上使用过。示例:$ CUDA_VISIBLE_DEVICES=1 python3 train_model.py 这将环境变量设置为仅检测第二个 GPU(它们通常从 id 0 开始,例如 0,1,2..),您应该能够按原样运行您的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多