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 "<ipython-input-7-91cdbd472507>", 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]
  • 5
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值