如何在 Google Colab 上安装 PyTorch v1.0.0+?

How do I install PyTorch v1.0.0+ on Google Colab?

PyTorch v1.0.0 稳定版为 released on 8 December 2018 after being announced 7 months earlier

我想要一个针对我的 IPython 内核 运行 所用的硬件优化的版本。

如何在 Google Colab 上获取此版本?

在 1.0.0 版本中,PyTorch 更改了下载 URL 格式:

https://download.pytorch.org/whl/cu92/torch-1.0.0-cp36-cp36m-linux_x86_64.whl

https://download.pytorch.org/whl/cu90/torch-1.0.0-cp36-cp36m-linux_x86_64.whl

(变化在CUDA版本部分,其中cu92变为cu90。)

为了以编程方式生成 URL,我使用了以下代码:

from os.path import exists
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag

platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
cuda_output = !ldconfig -p|grep cudart.so|sed -e 's/.*\.\([0-9]*\)\.\([0-9]*\)$/cu/'    
accelerator = cuda_output[0] if exists('/dev/nvidia0') else 'cpu'

torch_url=f"http://download.pytorch.org/whl/{accelerator}/torch-{version}-{platform}-linux_x86_64.whl"
version='1.0.0'

!pip install -U {torch_url} torchvision

然后您可以在发布新版本时根据需要更改 version 变量。

试试下面的代码片段(无论有没有 gpu,它都适用于运行时)

!pip install -q torch==1.0.0 torchvision

检查版本

import torch
print(torch.__version__)

这里是版本 1.0.0

更新

!pip install torch

现在工作正常,因为最稳定的版本是 1.0.0

对于版本 1.1.0,这有效

!pip install -q torch==1.1.0 torchvision

您现在可以

import torch

无需额外安装。

这是安装pytorch的代码。您可以将其更改为您想要的任何版本。

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

对我有用,你可以试试

!pip install torch
!pip install torchvision
!pip install mxnet-cu101