Google Colab 安装使用 PyTorch

编辑notebook设定,更改python版本并使用gpu

‘Edit’ –> ‘Notebook Settings’ :
‘runtime type’ –> python 3
‘hardware accelerator’ –> gpu

重要

每次新开一个notebook都需要重新设置以上步骤

安装pytorch官方命令安装pytorch

官方安装命令
http://pytorch.org/

!pip3 install http://download.pytorch.org/whl/cu80/torch-0.3.1-cp36-cp36m-linux_x86_64.whl 
Collecting torch==0.3.1 from http://download.pytorch.org/whl/cu80/torch-0.3.1-cp36-cp36m-linux_x86_64.whl
  Downloading http://download.pytorch.org/whl/cu80/torch-0.3.1-cp36-cp36m-linux_x86_64.whl (496.4MB)
    100% |████████████████████████████████| 496.4MB 45.0MB/s 
Requirement already satisfied: pyyaml in /usr/local/lib/python3.6/dist-packages (from torch==0.3.1)
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from torch==0.3.1)
Installing collected packages: torch
Successfully installed torch-0.3.1
!pip3 install torchvision
Collecting torchvision
  Downloading torchvision-0.2.0-py2.py3-none-any.whl (48kB)
    100% |████████████████████████████████| 51kB 1.8MB/s 
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from torchvision)
Requirement already satisfied: torch in /usr/local/lib/python3.6/dist-packages (from torchvision)
Collecting pillow>=4.1.1 (from torchvision)
  Downloading Pillow-5.0.0-cp36-cp36m-manylinux1_x86_64.whl (5.9MB)
    100% |████████████████████████████████| 5.9MB 236kB/s 
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from torchvision)
Requirement already satisfied: pyyaml in /usr/local/lib/python3.6/dist-packages (from torch->torchvision)
Installing collected packages: pillow, torchvision
  Found existing installation: Pillow 4.0.0
    Uninstalling Pillow-4.0.0:
      Successfully uninstalled Pillow-4.0.0
Successfully installed pillow-5.0.0 torchvision-0.2.0

重要2

在colab环境下,系统命令必须添加”!”符号才能正常使用,否则会提示错误

  File "", line 1
    pip install gdal
              ^
SyntaxError: invalid syntax

重要3

不要使用pip安装linux|pip|python3.5|cuda8版本,whl文件有问题

torch-0.3.1-cp35-cp35m-linux_x86_64.whl is not a supported wheel on this platform.

运行官方示例

x = torch.Tensor(5, 3)
print(x)
 6.3964e-37  0.0000e+00  4.4842e-44
 0.0000e+00         nan  1.3593e-43
 3.3234e-09  1.3517e+22  8.4576e+20
 6.6655e-10  1.3371e+22  3.3499e-09
 8.3097e+20  2.0682e+20  2.9387e-39
[torch.FloatTensor of size 5x3]

你可能感兴趣的:(深度学习)