PyTorch安装与基本使用详解

这篇文章主要介绍了PyTorch安装与基本使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

什么要学习PyTorch?

有的人总是选择,选择的人最多的框架,来作为自己的初学框架,比如Tensorflow,但是大多论文的实现都是基于PyTorch的,如果我们要深入论文的细节,就必须选择学习入门PyTorch

安装PyTorch

一行命令即可 官网

 pip install torch===1.6.0 torchvision===0.7.0 - https://download.pytorch.org/whl/torch_stable.html 

时间较久,耐心等待

测试自己是否安装成功

运行命令测试

 import torch x = torch.rand(5,3) print(x) 

输出

tensor([[0.5096, 0.1209, 0.7721],
        [0.9486, 0.8676, 0.2157],
        [0.0586, 0.3467, 0.5015],
        [0.9470, 0.5654, 0.9317],
        [0.2127, 0.2386, 0.0629]])

开始学习PyTorch

不初始化的创建张量

 import torch x = torch.empty([5,5]) print(x) 

输出

tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])

随机创建一个0-1的张量

 import torch x = torch.rand(5,5) print(x) 

输出

tensor([[0.3369, 0.5339, 0.8419, 0.6857, 0.6241],
        [0.4991, 0.1691, 0.8356, 0.4574, 0.0395],
        [0.9714, 0.2975, 0.9322, 0.5213, 0.8509],
        [0.3037, 0.8690, 0.3481, 0.2538, 0.9513],
        [0.0156, 0.9516, 0.3674, 0.1831, 0.6466]])

创建全为0的张量

 import torch x = torch.zeros(5,5, dtype=torch.float32) print(x) 

创建的时候可以通过dtype指定数据类型

输出

tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])

使用数据来直接创建张量

 import torch x = torch.zeros([5,5], dtype=torch.float32) print(x) 

输出

tensor([5., 5.])

使用原有tensor创建新的tensor

 import torch x = torch.tensor([5,5], dtype=torch.float32) x = x.new_zeros(5, 3) y = torch.rand_like(x) print(x) print(y) 

输出

tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
tensor([[0.5552, 0.3333, 0.0426],
        [0.3861, 0.3945, 0.6658],
        [0.6978, 0.3508, 0.4813],
        [0.8193, 0.2274, 0.8384],
        [0.9360, 0.9226, 0.1453]])

观察tensor的维度信息

 x = torch.rand(3,3) x.size()

输出

torch.Size([3, 3])

一些简单的运算

 x = torch.tensor([1]) y = torch.tensor([3]) ''' 方式1 ''' z = x + y ''' 方式2 ''' z = torch.add(x, y) ''' 方式3 ''' result = torch.empty(1) # 不初始化数据 torch.add(x, y, out=result) # 将结果返回到result中 ''' 方式4 ''' x.add_(y) 

输出

tensor([4])

索引操作

 x = torch.rand(5,5) x[:,:] x[1,:] x[:,1] x[1,1] 

分别输出

tensor([[0.4012, 0.2604, 0.1720, 0.0996, 0.7806],
        [0.8734, 0.9087, 0.4828, 0.3543, 0.2375],
        [0.0924, 0.9040, 0.4408, 0.9758, 0.2250],
        [0.7179, 0.7244, 0.6165, 0.1142, 0.7363],
        [0.8504, 0.0391, 0.0753, 0.4530, 0.7372]])
tensor([0.8734, 0.9087, 0.4828, 0.3543, 0.2375])
tensor([0.2604, 0.9087, 0.9040, 0.7244, 0.0391])
tensor(0.9087)

维度变换

 x = torch.rand(4,4) x.view(16) x.view(8,2) x.view(-1,8)

分别输出

tensor([0.9277, 0.9547, 0.9487, 0.9841, 0.4114, 0.1693, 0.8691, 0.3954, 0.4679,
        0.7914, 0.7456, 0.0522, 0.0043, 0.2097, 0.5932, 0.9797])
tensor([[0.9277, 0.9547],
        [0.9487, 0.9841],
        [0.4114, 0.1693],
        [0.8691, 0.3954],
        [0.4679, 0.7914],
        [0.7456, 0.0522],
        [0.0043, 0.2097],
        [0.5932, 0.9797]])
tensor([[0.9277, 0.9547, 0.9487, 0.9841, 0.4114, 0.1693, 0.8691, 0.3954],
        [0.4679, 0.7914, 0.7456, 0.0522, 0.0043, 0.2097, 0.5932, 0.9797]])

注意:必须维度变换数据的数量必须保持一致

到此这篇关于PyTorch安装与基本使用详解的文章就介绍到这了,更多相关PyTorch安装与使用内容请搜索html中文网以前的文章或继续浏览下面的相关文章希望大家以后多多支持html中文网!

以上就是PyTorch安装与基本使用详解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python