pytorch概述
PyTorch 是⼀个开源的深度学习框架,由 Facebook 的⼈⼯智能研究团队开发和维护,于2017年在GitHub上开源,在学术界和⼯业界都得到了⼴泛应⽤
pytorch能做什么
pytorch基础
量的概念
- 标量:数字1,2,3
- 向量:一维表格[1,2,3]
- 矩阵:二维表格[(1,2),(3,4)]
通过向量、矩阵形貌的物体最多是H*W维,而生存中许多东西有更高维度,就用张量表示
前面三种量也可以当成张量的一种
张量(Tensor)的根本概念
张量(Tensor)是pytorch中的根本单位,也是深度学习框架构成的重要构成。
我们可以先把张量看做是⼀个容器,⾥⾯承载了必要运算的数据。
tensor 即“张量”。实际上跟numpy数组、向量、矩阵的格式根本一样。但是是专门针对GPU来设计的,可以运行在GPU上来加快计算效率
在PyTorch中,张量Tensor是最基础的运算单位,与NumPy中的NDArray类似,张量表示的是一个多维矩阵。不同的是,PyTorch中的Tensor可以运行在GPU上,而NumPy的NDArray只能运行在CPU上。由于Tensor能在GPU上运行,因此大大加快了运算速率。
一句话总结:一个可以运行在gpu上的多维数据而已
样本和模型 --> Y=WX+B
X:表示样本
W、B:表示变量
Y:表示标签
张量的类型
Data typedtypeLegacy Constructors(type)32-bit floating pointtorch.float32 or torch.floattorch.*.FloatTensor64-bit floating pointtorch.float64 or torch.doubletorch.*.DoubleTensor64-bit complextorch.complex64 or torch.cfloat128-bit complextorch.complex128 or torch.cdouble16-bit floating pointtorch.float16 or torch.halftorch.*.HalfTensor16-bit floating pointtorch.bfloat16torch.*.BFloat16Tensor8-bit integer (无符号)torch.uint8torch.*.ByteTensor8-bit integer (有符号)torch.int8torch.*.CharTensor16-bit integer (有符号)torch.int16 or torch.shorttorch.*.ShortTensor32-bit integer (有符号)torch.int32 or torch.inttorch.*.IntTensor64-bit integer (有符号)torch.int64
or torch.longtorch.*.LongTensorBoolean(布尔型)torch.booltorch.*.BoolTensor 张量的创建
函数功能Tensor(*size)基础构造函数Tensor(data)类似np.arrayones(*size)全1 Tensorzeros(*size)全0 Tensoreye(*size)对角线为1,其他为0arange(s,e,step)从s到e,步长为step的等差数列(不包含e这个值)linspace(s,e,steps)从s到e,匀称切分成steps份,steps是值的个数rand/randn(*size)匀称/尺度分布normal(mean,std)/uniform_(from,to)正态分布/匀称分布randperm(m)随机分列 张量初始化方法
1.直接从数据,张量可以直接从数据中创建。数据类型是⾃动推断的
- data = [[1, 2],[3, 4]]
- x_data = torch.tensor(data)
- x_data
复制代码 2.从numpy数组中创建张量(反之亦然)
- import numpy as np
- np_array = np.array(data)
- x_np = torch.from_numpy(np_array)
复制代码 3.从另一个张量创建张量[除非明确覆盖,否则新张量保留参数张量的属性]
- x_ones = torch.ones_like(x_data) # 保留x_data的属性
- print(f"Ones Tensor: \n {x_ones} \n")
- #由于x_data的数据类型是int64,rand_like函数会生成一个随机张量,数据类型与x_data相同
- #而torch.rand()方法是创建一个服从均匀分布的随机张量,值在 [0, 1),数据类型是float32,所以需要强制转换
- x_rand = torch.rand_like(x_data, dtype=torch.float) # 重写x_data的数据类型
- print(f"Random Tensor: \n {x_rand} \n")
复制代码- Ones Tensor:
- tensor([[1, 1],
- [1, 1]])
- Random Tensor:
- tensor([[0.3156, 0.5076],
- [0.8555, 0.4440]])
复制代码 4.使用随机值或常量值
shape 是张量维度的元组。在下⾯的函数中,它决定了输出张量的维度
- shape = (2,3,) # 一个标量
- rand_tensor = torch.rand(shape)
- ones_tensor = torch.ones(shape)
- zeros_tensor = torch.zeros(shape)
- print(f"Random Tensor: \n {rand_tensor} \n")
- print(f"Ones Tensor: \n {ones_tensor} \n")
- print(f"Zeros Tensor: \n {zeros_tensor}")
复制代码- Random Tensor:
- tensor([[0.5733, 0.8237, 0.1398],
- [0.9530, 0.9231, 0.2764]])
- Ones Tensor:
- tensor([[1., 1., 1.],
- [1., 1., 1.]])
- Zeros Tensor:
- tensor([[0., 0., 0.],
- [0., 0., 0.]])
复制代码 5.其他一些创建方法
基于现有tensor构建,但使⽤新值添补
- m = torch.ones(5,3, dtype=torch.double)
- n = torch.rand_like(m, dtype=torch.float)
- # 获取tensor的⼤⼩
- print(m.size()) # torch.Size([5,3])
- # 均匀分布
- torch.rand(5,3)
- # 标准正态分布
- torch.randn(5,3)
- # 离散正态分布
- torch.normal(mean=.0,std=1.0,size=([5,3]))
- # 线性间隔向量(返回⼀个1维张量,包含在区间start和end上均匀间隔的steps个点) 等差数列
- torch.linspace(start=1,end=10,steps=20)
复制代码- torch.Size([5, 3])
- tensor([ 1.0000, 1.4737, 1.9474, 2.4211, 2.8947, 3.3684, 3.8421, 4.3158,
- 4.7895, 5.2632, 5.7368, 6.2105, 6.6842, 7.1579, 7.6316, 8.1053,
- 8.5789, 9.0526, 9.5263, 10.0000])
复制代码 张量的属性
每个Tensor有torch.dtype、torch.device、torch.layout三种属性
torch.device标识了torch.Tensor对象在创建之后所存储在的设备名称(cpu照旧GPU)
torch.layout表示torch.Tensor内存布局的对象
张量的属性形貌了张量的形状、数据类型和存储它们的设备。
以对象的⻆度来判断,张量可以看做是具有特性和⽅法的对象
- tensor = torch.rand(3,4)
- print(f"Shape of tensor: {tensor.shape}")
- print(f"Datatype of tensor: {tensor.dtype}")
- print(f"Device tensor is stored on: {tensor.device}")
复制代码- Shape of tensor: torch.Size([3, 4])
- Datatype of tensor: torch.float32
- Device tensor is stored on: cpu
复制代码 张量运算
官网总结的100多种张量运算包括算术、线性代数、矩阵操作(转置、索引、切⽚)、采样等等
这些操作中的每⼀个都可以在 GPU 上运⾏(速率通常⽐在 CPU 上更快)
默认情况下,张量是在 CPU 上创建的。
我们可以使⽤使⽤ .to() ⽅法明确地将张量移动到 GPU (GPU可⽤的情况下)。
请留意!跨设备复制内容量较⼤的张量,在时间和内存⽅⾯大概成本很⾼!
- # 设置张量在GPU上运算
- # We move our tensor to the GPU if available
- if torch.cuda.is_available():
- tensor = tensor.to('cuda')
复制代码 张量的索引和切片
- tensor = torch.ones(4, 4) # 创建一个4x4的张量
- print('First row: ', tensor[0]) # 打印第一行
- print('First column: ', tensor[:, 0]) # 打印第一列
- print('Last column:', tensor[..., -1]) # 打印最后一列
- tensor[:,1] = 0 # 第二列赋值为0
- print(tensor)
复制代码- First row: tensor([1., 1., 1., 1.])
- First column: tensor([1., 1., 1., 1.])
- Last column: tensor([1., 1., 1., 1.])
- tensor([[1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.]])
复制代码 张量的拼接
可以使⽤ torch.cat ⽤来连接指定维度的⼀系列张量。另⼀个和 torch.cat 功能类似的函数是torch.stack
方法含义格式torch.cat沿现有维度连接给定的序列torch.cat(tensor, dim = 0 , *, out = None )torch.stack沿新维度连接一系列张量torch.stack(张量, dim = 0, *, out = None)- print(tensor) # 打印原始张量
- t1 = torch.cat([tensor, tensor, tensor], dim=1) # 按列拼接
- # dim 参数决定了拼接操作沿着哪个维度进行。具体来说:
- # • dim=-1 表示沿着最后一个维度拼接
- # • dim=0 表示沿着第一个维度(行的方向)拼接。
- # • dim=1 表示沿着第二个维度(列的方向)拼接。
- # • dim=2 表示沿着第三个维度(深度方向,通常是针对三维张量)拼接,以此类推。
- print(t1)
复制代码- tensor([[1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.]])
- tensor([[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
- [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
- [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
- [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.]])
复制代码 算数运算
加法运算
- # 加法运算
- t1 = torch.tensor([[1,2],[3,4]])
- print(t1)
- t2 = torch.tensor([[5,6],[7,6]])
- print(t2)
- t3 = t1 + t2
- print(t3)
- t4 = torch.add(t1, t2)
- print(t4)
- print(t1.add(t2))
- print(t1)
- #t1.add_(t2) # 会改变t1的值
复制代码- tensor([[1, 2],
- [3, 4]])
- tensor([[5, 6], [7, 6]])tensor([[ 6, 8], [10, 10]])tensor([[ 6, 8], [10, 10]])tensor([[ 6, 8], [10, 10]])tensor([[1, 2],
- [3, 4]])
复制代码 减法运算
- #减法运算
- print(t1 - t2)
- print(torch.sub(t1, t2))
- print(t1.sub(t2))
- print(t1)
复制代码- tensor([[-4, -4], [-4, -2]])tensor([[-4, -4], [-4, -2]])tensor([[-4, -4], [-4, -2]])tensor([[1, 2],
- [3, 4]])
复制代码 乘法运算
计算两个张量之间矩阵乘法的⼏种⽅式。 y1, y2, y3 末了的值是⼀样的
二维矩阵乘法运算包括torch.mm(),torch.matmul()(高维度仅支持),@
对于高维度的Tensor(dim>2),界说其矩阵乘法仅在末了的两个维度上,要求前面的维度必须保持同等,就像矩阵的索引一样并且运算操作只有torch.matul()
- print(tensor) # 打印原始张量
- y1 = tensor @ tensor.T
- print(y1) # 等价于 tensor.matmul(tensor.T)
- y2 = tensor.matmul(tensor.T)
- print(y2) # 等价于 tensor @ tensor.T
- y3 = torch.rand_like(tensor) # 与tensor形状相同的随机张量(初始化y3)
- torch.matmul(tensor, tensor.T, out=y3) # 输出到y3
- print(y3)
复制代码- tensor([[1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.]])
- tensor([[3., 3., 3., 3.],
- [3., 3., 3., 3.],
- [3., 3., 3., 3.],
- [3., 3., 3., 3.]])
- tensor([[3., 3., 3., 3.],
- [3., 3., 3., 3.],
- [3., 3., 3., 3.],
- [3., 3., 3., 3.]])
- tensor([[3., 3., 3., 3.],
- [3., 3., 3., 3.],
- [3., 3., 3., 3.],
- [3., 3., 3., 3.]])
复制代码- #高维度矩阵运算
- t5 = torch.ones(1,2,3,4)
- print(t5)
- t6 = torch.ones(1,2,4,3)
- print(t6)
- print(t5.matmul(t6)) # torch.Size([1, 2, 3, 1, 2, 3])
- print(torch.matmul(t5, t6)) # torch.Size([1, 2, 3, 1, 2, 3])
复制代码- tensor([[[[1., 1., 1., 1.],
- [1., 1., 1., 1.],
- [1., 1., 1., 1.]],
- [[1., 1., 1., 1.],
- [1., 1., 1., 1.],
- [1., 1., 1., 1.]]]])
- tensor([[[[1., 1., 1.],
- [1., 1., 1.],
- [1., 1., 1.],
- [1., 1., 1.]],
- [[1., 1., 1.],
- [1., 1., 1.],
- [1., 1., 1.],
- [1., 1., 1.]]]])
- tensor([[[[4., 4., 4.],
- [4., 4., 4.],
- [4., 4., 4.]],
- [[4., 4., 4.],
- [4., 4., 4.],
- [4., 4., 4.]]]])
- tensor([[[[4., 4., 4.],
- [4., 4., 4.],
- [4., 4., 4.]],
- [[4., 4., 4.],
- [4., 4., 4.],
- [4., 4., 4.]]]])
复制代码 计算张量逐元素相乘的⼏种⽅法。 z1, z2, z3 末了的值是⼀样的
哈达码积(element wise,对应元素相乘)
- print(tensor) # 打印原始张量
- z1 = tensor * tensor # 逐元素相乘
- print(z1) # 等价于 tensor.mul(tensor)
- z2 = tensor.mul(tensor) # 逐元素相乘
- print(z2) # 等价于 tensor * tensor
- z3 = torch.rand_like(tensor) # 与tensor形状相同的随机张量(初始化z3)
- torch.mul(tensor, tensor, out=z3) # 输出到z3
- print(z3)
复制代码- tensor([[1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.]])
- tensor([[1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.]])
- tensor([[1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.]])
- tensor([[1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.]])
复制代码 除法运算
- #除法运算
- print(t1 / t2)
- print(torch.div(t1, t2))
- print(t1.div(t2))
- print(t1)
复制代码- tensor([[0.2000, 0.3333], [0.4286, 0.6667]])tensor([[0.2000, 0.3333], [0.4286, 0.6667]])tensor([[0.2000, 0.3333], [0.4286, 0.6667]])tensor([[1, 2],
- [3, 4]])
复制代码 幂运算
使用torch.pow(tensor,2);**;两种方法
e指函数:torch.exp(tensor)
- print(t1)
- print(torch.pow(t1, 2)) # 每个元素平方
- print(t1.pow(2)) # 每个元素平方
- print(t1**2) # 每个元素平方
- #print(t1.pow_(2)) # 每个元素平方
复制代码- tensor([[1, 2],
- [3, 4]])
- tensor([[ 1, 4], [ 9, 16]])tensor([[ 1, 4], [ 9, 16]])tensor([[ 1, 4], [ 9, 16]])
复制代码 开方运算
tensor.sqrt()
tensor.sqrt_()
对数运算
torch.log2(tensor)
torch.log10(tensor)
torch.log(tensor)
torch.log_(tensor)
单位素张量
如果⼀个单位素张量,比方将张量的值聚合计算,可以使⽤ item() ⽅法将其转换为Python 数值
- print(tensor) # 打印原始张量
- agg = tensor.sum() # 求和
- print(agg)
- agg_item = agg.item() # 将张量的值转换为Python数值
- print(agg_item, type(agg_item)) # 打印agg_item的值和类型
复制代码- tensor([[1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.],
- [1., 0., 1., 1.]])
- tensor(12.)
- 12.0 <class 'float'>
复制代码 In-place操作
把计算效果存储到当前操作数中的操作就称为就地操作。含义和pandas中inPlace参数的含义⼀样。pytorch中,这些操作是由带有下划线 _ 后缀的函数表⽰。
比方:x.copy_(y) , x.t_() , 将改变 x ⾃⾝的值
In-place操作虽然节省了⼀部分内存,但在计算导数时大概会出现问题,因为它会⽴即丢失历史记载。因此,不⿎励使⽤它们。
- x = torch.tensor([(1, 2, 3), (4, 5, 6), (7, 8, 9)])
- print(x)
- x.add_(2) # 逐元素加2
- print(x) # 打印x的值
- # 注意:任何以`_`结尾的操作都会用结果替换原始张量。例如:x.copy_(y), x.t_() , 将更改 `x`.
复制代码- tensor([[1, 2, 3],
- [4, 5, 6],
- [7, 8, 9]])
- tensor([[ 3, 4, 5],
- [ 6, 7, 8],
- [ 9, 10, 11]])
复制代码 与numpy之间的转换
CPU 和 NumPy 数组上的张量共享底层内存位置,以是改变⼀个另⼀个也会变
张量到numpy数组
- t1 = torch.ones(6) # 创建一个张量
- print(f"t1:{t1}") #这里的f是格式化张量的内容到字符串中
- n1 = t1.numpy() # 张量转numpy数组
- print(f"n1:{n1}") # 打印numpy数组
复制代码- t1:tensor([1., 1., 1., 1., 1., 1.])
- n1:[1. 1. 1. 1. 1. 1.]
复制代码- t1.add_(1) # 逐元素加1
- print(f"t1:{t1}") # 打印张量
- print(f"n1:{n1}") # 打印numpy数组
复制代码- t1:tensor([2., 2., 2., 2., 2., 2.])
- n1:[2. 2. 2. 2. 2. 2.]
复制代码 Numpy数组到张量
- n2 = np.ones(5) # 创建一个numpy数组
- print(f"n2:{n2}") # 打印numpy数组
- t2 = torch.from_numpy(n2) # numpy数组转张量
- print(f"t2:{t2}") # 打印张量
- #Numpy数组和PyTorch张量将共享它们的底层内存位置,因此对一个进行更改将导致另一个也发生更改。
- np.add(n2,1,out=n2) # 逐元素加1
- print(f"t2:{t2}") # 打印张量
- print(f"n2:{n2}") # 打印numpy数组
复制代码- n2:[1. 1. 1. 1. 1.]
- t2:tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
- t2:tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
- n2:[2. 2. 2. 2. 2.]
复制代码 计算图
在进⼀步学习pytorch之前,先要相识⼀个概念 —— 计算图( Computation graph)所有的深度学习框架都依赖于计算图来完成梯度降落、优化梯度值等计算。
⽽计算图的创建和应⽤,通常包含如下两个部分:
模型从简单到复杂,pytorch和tensorflow都使⽤计算图来完成⼯作。
但是,这两个框架所使⽤的计算图也却有所不同:
tensorflow1.x 使⽤的是静态计算图,tensorflow2.x和pytorch使⽤的是动态计算图
静态计算图
先搭建计算图,后运行;允许编译器举行优化
通常包括以下两个阶段。
- 界说⼀个架构(可以使⽤⼀些根本的流控制⽅法,⽐如循环和条件指令)
- 运⾏⼀组数据来训练模型,进⾏推理
- 优点:允许对图进⾏强⼤的离线优化/调度,以是速率相对较快。
- 缺点:难以调试,对代码中处理布局化或者可变⼤⼩的数据处理⽐较复杂
动态计算图
编好程序即可执行
在执⾏正向计算时,隐式地界说图(动态构建)。
- 优点:机动,侵⼊性⼩,允许动态构建和评估
- 缺点:难以优化
两种计算图⽐较起来,可以看出:动态图是对调试友好的(对程序员友好)。它允许逐⾏执⾏代码,并可以访问所有张量。这样更便于发现和找到我们计算或逻辑中的问题
pytorch计算图可视化
通过torchviz可以实现
- import torch
- from torchviz import make_dot
- # 定义矩阵 A,向量 b 和常数 c
- A = torch.randn(10, 10,requires_grad=True) #requires_grad=True表示需要计算梯度,对A求导
- b = torch.randn(10,requires_grad=True)
- c = torch.randn(1,requires_grad=True)
- x = torch.randn(10, requires_grad=True)
- # 计算 x^T * A + b * x + c
- result = torch.matmul(A, x.T) + torch.matmul(b, x) + c
- # ⽣成计算图节点
- dot = make_dot(result, params={'A': A, 'b': b, 'c': c, 'x': x})
- # 绘制计算图
- dot.render('expression', format='png', cleanup=True, view=False)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |