PyTorch的基本概念
1.什么是Pytorch,为什么选择Pytroch?
PyTorch是一个基于python的科学计算包,主要针对两类人群:
a.作为NumPy的替代品,可以利用GPU的性能进行计算
b.作为一个高灵活性、速度快的深度学习平台
选择PyTorch,因为PyTorch是当前难得的简洁优雅且高效快速的框架。下面是许多研究人员选择
PyTorch的原因。
① 简洁:PyTorch的设计追求最少的封装,尽量避免重复造轮子。PyTorch的设计遵循tensor→variable(autograd)→nn.Module 三个由低到高的抽象层次,分别代表高维数组(张量)、自动求导(变量)和神经网络(层/模块),而且这三个抽象之间联系紧密,可以同时进行修改和操作。使得PyTorch的源码十分易于阅读。
② 速度:PyTorch的灵活性不以速度为代价,在许多评测中,PyTorch的速度表现胜过TensorFlow和Keras等框架。
③易用:PyTorch是所有的框架中面向对象设计的最优雅的一个。PyTorch的面向对象的接口设计来源于Torch,而Torch的接口设计以灵活易用而著称,PyTorch继承了Torch的衣钵,尤其是API的设计和模块的接口都与Torch高度一致。PyTorch的设计最符合人们的思维,它让用户尽可能地专注于实现自己的想法,即所思即所得,不需要考虑太多关于框架本身的束缚。
④活跃的社区:PyTorch提供了完整的文档,循序渐进的指南,作者亲自维护的论坛 供用户交流和求教问题,还有Facebook 人工智能研究院对PyTorch提供了强力支持。
2.pytorch安装
2.1 anaconda下载与安装
在anaconda环境下进行安装的,
首先下载anaconda,点击download
2.2 环境配置
下载完之后,进行创建虚拟环境。
首先找到anaconda prompt
然后输入命令
创建环境
conda create -n name [python=3.x]
以上name为你自己想取得名字,[]里面可以指定python或者其他包版本,若不知指定python版本 则默认安装最新的python版本。
激活环境
conda activate env_name
安装pytorch
https://pytorch.org/get-started/locally/
2.3 环境测试
安装测试
第一行为:测试是否安装pytorch成功
第二行为:测试pytoch的gpu版本是否成功
3.pytorch基础知识
构建随机初始化矩阵
x = torch.rand(5, 3)
print(x)
Out:
tensor([[0.5728, 0.5375, 0.0494],
[0.2820, 0.1853, 0.8619],
[0.0856, 0.8380, 0.8117],
[0.7959, 0.8802, 0.3610],
[0.4440, 0.4028, 0.2289]])
构建一个矩阵都是0,类型为long
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
out
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
直接从数据中构建张量
x = torch.tensor([5.5, 3])
print(x)
out
tensor([5.5000, 3.0000])
或者根据现有的张量创建张量。 这些方法将重用输入张量的属性,例如, dtype,除非用户提供新值
x = x.new_ones(5, 3, dtype=torch.double) # new_* methods take in sizes
print(x)
x = torch.randn_like(x, dtype=torch.float) # override dtype!
print(x)
Out:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[ 0.3928, -0.4377, -0.6426],
[ 0.6000, 0.1942, -0.9790],
[-3.0629, 0.2410, -1.5378],
[ 0.0219, 0.5899, 0.8386],
[-0.1540, 0.2724, 0.3881]])
获得其大小
print(x.size())
out:
torch.Size([5, 3])
注意:torch.Size实际上是一个元组,因此它支持所有元组操作。
1.2 基本操作(加、减等)
操作有多种语法。 在下面的示例中,我们将查看添加操作。
加:第一种:
y = torch.rand(5, 3)
print(x + y)
out:
tensor([[ 1.0653, 0.1367, -0.1789],
[ 1.2751, 0.3061, -0.2860],
[-2.9511, 0.3313, -1.5280],
[ 0.5614, 1.4668, 1.7641],
[ 0.1087, 0.8200, 0.5692]])
加:第二种
print(torch.add(x, y))
Out:
tensor([[ 1.0653, 0.1367, -0.1789],
[ 1.2751, 0.3061, -0.2860],
[-2.9511, 0.3313, -1.5280],
[ 0.5614, 1.4668, 1.7641],
[ 0.1087, 0.8200, 0.5692]])
加法:提供输出张量作为参数
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
Out:
tensor([[ 1.0653, 0.1367, -0.1789],
[ 1.2751, 0.3061, -0.2860],
[-2.9511, 0.3313, -1.5280],
[ 0.5614, 1.4668, 1.7641],
[ 0.1087, 0.8200, 0.5692]])
加法,in-place(改变原有的)
adds x to y
y.add_(x)
print(y)
Out:
tensor([[ 1.0653, 0.1367, -0.1789],
[ 1.2751, 0.3061, -0.2860],
[-2.9511, 0.3313, -1.5280],
[ 0.5614, 1.4668, 1.7641],
[ 0.1087, 0.8200, 0.5692]])
任何用_后固定的操作。 例如:x.copy_(y),x.t_(),将改变x。
Resizing(改变张量大小):如果你想调整/重塑张量,你可以使用torch.view:
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())
out:
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
如果你有一个元素张量,使用.item()将值作为Python数字
x = torch.randn(1)
print(x)
print(x.item())
Out:
tensor([-1.7816])
-1.7815848588943481
Numpy 和Tensor转换
将Torch Tensor转换为NumPy阵列(反之亦然)是一件轻而易举的事。
Converting a Torch Tensor to a NumPy Array(将tensor转换为Numpy)
a = torch.ones(5)
print(a)
Out:
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
print(b)
out:
[1. 1. 1. 1. 1.]
a.add_(1)
print(a)
print(b)
out:
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
注意:Torch Tensor和NumPy阵列将共享其底层内存位置,而更改一个将改变其他位置
Numpy 转换为Tensor
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
out:
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
除了CharTensor之外,CPU上的所有Tensors都支持转换为NumPy并返回。