【问题标题】:How can I enable pytorch GPU support in Google Colab?如何在 Google Colab 中启用 pytorch GPU 支持?
【发布时间】:2018-11-10 12:00:09
【问题描述】:

如何让 pytorch 在 GPU 上工作?

我已经在 google colab notebook 中成功安装了 pytorch: TensorFlow 报告 GPU 已就位:

但 torch.device 功能以某种方式失败:

我该如何解决这个问题?

【问题讨论】:

标签: gpu pytorch google-colaboratory


【解决方案1】:

我遇到了同样的问题。

尝试像这样安装 Torch:

# http://pytorch.org/
from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())

accelerator = 'cu80' #'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'
print('Platform:', platform, 'Accelerator:', accelerator)

!pip install --upgrade --force-reinstall -q http://download.pytorch.org/whl/{accelerator}/torch-0.4.0-{platform}-linux_x86_64.whl torchvision

import torch
print('Torch', torch.__version__, 'CUDA', torch.version.cuda)
print('Device:', torch.device('cuda:0'))

输出应该是:

平台:cp36-cp36m 加速器:cu80 Torch 0.4.0 CUDA 8.0.61
设备:cuda:0

一些浮动的sn-ps使用torch-0.3.0.post4-{platform}-linux_x86_64.whl,这会导致同样的错误,因为device是Torch 4的一个特性。如果你已经安装了错误的版本,你可能需要做!pip uninstall torch

还要确保在编辑>笔记本设置>硬件加速器下启用GPU

【讨论】:

    【解决方案2】:

    您可以使用本教程:https://medium.com/@nrezaeis/pytorch-in-google-colab-640e5d166f13

    例如对于 CUDA 9.2 和 Python 3.6:

    !pip3 install http://download.pytorch.org/whl/cu92/torch-0.4.1-cp36-cp36m-linux_x86_64.whl
    !pip3 install torchvision
    

    现在使用 PyTorch 检查 GPU 设备:

    torch.cuda.get_device_name(0)
    

    我在 Google Colab 中的结果是 Tesla K80。

    【讨论】:

      【解决方案3】:

      您可以通过单击“运行时”菜单下的“更改运行时类型”来启用 GPU。这些天还提供“TPU”支持。

      您可以使用torch.device 定义device

      import torch
      
      DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
      

      【讨论】:

        【解决方案4】:

        除了在“运行时”菜单下启用 GPU -> 更改运行时类型之外,还通过以下方式启用 GPU 支持:

        import torch
        
        if torch.cuda.is_available():
          device = torch.device("cuda")
        else:
          device = torch.device("cpu")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-01-07
          • 2018-11-06
          • 2021-04-12
          • 2020-02-25
          • 2020-04-14
          • 2020-08-28
          • 2019-02-07
          相关资源
          最近更新 更多