PyTorch1.0安装教程(附三个测试示例)

Summary:Python1.0安装教程

Author:Amusi

Date:2018-12-20

github:https://github.com/amusi/PyTorch-From-Zero-To-One

知乎:https://www.zhihu.com/people/amusi1994

微信公众号:CVer

本文是在Ubuntu下进行PyTorch1.0正式版的安装,Windows安装教程与之类似,也可以参考该教程进行安装:https://blog.csdn.net/amusi1994/article/details/80077667

环境说明

  • OS:Ubuntu16.04
  • CUDA:8.0
  • cudnn:6.0
  • Python(conda):3.6.4

安装教程

官网:https://pytorch.org/

检查Python环境

在这里插入图片描述

根据当前系统环境点击选项

在这里插入图片描述

在终端输入匹配的安装PyTorch1.0的命令

conda install pytorch torchvision cuda80 -c pytorch

回车进行安装,此时会有如下提示,当搜索到PyTorch1.0的相关packages时,输入 y,确定继续安装。

注:此时可能会找不到相应的packages,比如Windows环境下。所以你可以添加相关的搜索源,如清华的源。此处可以自行百度解决。

在这里插入图片描述

此时需要等待一会儿(具体看网速),因为PyTorch 1.0.0这个packages有437.5 MB大小。

在这里插入图片描述

安装成功后,会提示done。

在这里插入图片描述

加载PyTorch并输出版本号,验证是否安装成功。

python
import torch
print(torch.__version__)

在这里插入图片描述

测试示例

测试1:检查CUDA和CUDNN

创建并打开新的脚本文件pytorch_cudn_cudnn_test.py

touch pytorch_cudn_cudnn_test.py
gedit pytorch_cudn_cudnn_test.py 

写入测试代码

# Summary: 检测当前Pytorch和设备是否支持CUDA和cudnn
# Author:  Amusi
# Date:    2018-12-20 
# github:  https://github.com/amusi/PyTorch-From-Zero-To-One

import torch
 
if __name__ == '__main__':
	print("Support CUDA ?: ", torch.cuda.is_available())
	x = torch.Tensor([1.0])
	xx = x.cuda()
	print(xx)
 
	y = torch.randn(2, 3)
	yy = y.cuda()
	print(yy)
 
	zz = xx + yy
	print(zz)
 
	# CUDNN TEST
	from torch.backends import cudnn
	print("Support cudnn ?: ",cudnn.is_acceptable(xx))

运行该测试代码

python pytorch_cudn_cudnn_test.py

输入结果如下:

在这里插入图片描述

测试2:Tensors

创建并打开新的脚本文件pytorch_tensors.py

touch pytorch_tensors.py
gedit pytorch_tensors.py

写入测试代码:

# Summary:PyTorch的Tensor基础知识
# Author:  Amusi
# Date:    2018-12-20 
# github:  https://github.com/amusi/PyTorch-From-Zero-To-One
# Reference: http://pytorch.org/tutorials/beginner/pytorch_with_examples.html#pytorch-tensors
 
import torch
 
dtype = torch.FloatTensor
# dtype = torch.cuda.FloatTensor # Uncomment this to run on GPU
 
# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10
 
# Create random input and output data
x = torch.randn(N, D_in).type(dtype)
y = torch.randn(N, D_out).type(dtype)
 
# Randomly initialize weights
w1 = torch.randn(D_in, H).type(dtype)
w2 = torch.randn(H, D_out).type(dtype)
 
learning_rate = 1e-6
for t in range(500):
    # Forward pass: compute predicted y
    h = x.mm(w1)
    h_relu = h.clamp(min=0)
    y_pred = h_relu.mm(w2)
 
    # Compute and print loss
    loss = (y_pred - y).pow(2).sum()
    print(t, loss)
 
    # Backprop to compute gradients of w1 and w2 with respect to loss
    grad_y_pred = 2.0 * (y_pred - y)
    grad_w2 = h_relu.t().mm(grad_y_pred)
    grad_h_relu = grad_y_pred.mm(w2.t())
    grad_h = grad_h_relu.clone()
    grad_h[h < 0] = 0
    grad_w1 = x.t().mm(grad_h)
 
    # Update weights using gradient descent
    w1 -= learning_rate * grad_w1
    w2 -= learning_rate * grad_w2

运行该测试代码

python pytorch_tensors.py

输入结果如下:

在这里插入图片描述

测试3:MNIST

创建并打开新的脚本文件pytorch_mnist.py

touch pytorch_mnist.py
gedit pytorch_mnist.py

写入测试代码:

# Summary: 使用PyTorch玩转MNIST 
# Author:  Amusi
# Date:    2018-12-20 
  • 5
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值