相关资源
在教材的导航栏点击【MXNet】或【PyTorch】下载对应离线版本,点击【Jupter记事本】下载源码
深度学习基础
第一节:数据操作
三维数组解释:把RGB通道的三层分别视为3列,叠加起来
四维数组解释:读入图片不是一张一张读,而是一批一批读,叫一个批量
子区域[1:3,1:]
解释:前面1:3表示从第一行到第三行,左闭右开,所以就是取了第1行和第2行,后面1:表示从第一列拿到最后一列,在本例中就是第1列,第2列和第3列。
子区域[::3,::2]
解释:跳着访问,::3表示每3行一跳,在本例中就是取了第0行和第3行,::2表示每2列一跳,在本例中就是取了第0列和第2列。
张量表示由一个数值组成的数组,这个数组可能有多个维度。
创建数组需要:
- 形状:例如 3 × 4 3\times 4 3×4矩阵
- 每个元素的数据类型,例如32位浮点数
- 每个元素的值,例如全是0,或者随机数
导入torch
import torch
torch.arange()函数
torch.arange(start, end, step=1, out=None)
返回一个1维张量,长度为 f l o o r ( ( e n d − s t a r t ) / s t e p ) floor((end-start)/step) floor((end−start)/step)包含从start到end,以step为步长的一组序列值(默认start为0,步长为1)
参数:
- start (float) – 序列的起始点
- end (float) – 序列的终止点
- step (float) – 相邻点的间隔大小
- out (Tensor, optional) – 结果张量
指定end:
x = torch.arange(12)
print(x)
print(x.dtype)
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
torch.int64
指定:start和out:
x = torch.arange(1, 12)
print(x)
tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
指定start、end和step:
x = torch.arange(1, 12, 3)
print(x)
tensor([ 1, 4, 7, 10])
指定start和dtype:
x = torch.arange(12, dtype=torch.float32)
print(x)
tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.])
torch.float32
pytorch里一般使用torch.arange(),而不使用torch.range(),关于两者的区别:【pytorch.range() 和 pytorch.arange() 的区别】
访问张量的形状和元素总数
x = torch.arange(12)
print(x)
# 访问张量形状
print(x.shape)
print(x.size())
# 访问张量长度
print(len(x))
# 访问张量中元素总数
print(x.numel())
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
torch.Size([12])
torch.Size([12])
12
12
torch.reshape()函数
torch.reshape()接受的参数依次为最高维,次高维,…,第三维(通道),第二维(行),第一维(列)
x = torch.arange(12).reshape(3, 4)
print(x)
# 访问张量形状
print(x.shape)
print(x.size())
# 访问张量长度
print(len(x))
# 访问张量中元素总数
print(x.numel())
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
torch.Size([3, 4])
torch.Size([3, 4])
3
12
tensor([[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11]]])
使用全0,全1,其他常量或从特定分布中随机采样的数字
x = torch.zeros(2, 3, 4)
print(x)
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.]]])
x = torch.ones(2, 3, 4)
print(x)
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.]]])
通过提供包含数值的列表(或嵌套列表)来为所需张量中的每个元素赋予确定值。在这⾥,最外层的列表对应于轴0,内层的列表对应于轴1:
x = torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
print(x)
print(x.sum())
tensor([[2, 1, 4, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]])
tensor(30)
torch.randn()方法从均值为0、标准差为1的标准⾼斯(正态)分布中随机采样得到元素:
x = torch.randn(3, 4)
print(x)
tensor([[-1.0596, -1.1175, -0.0242, 1.0464],
[-1.3995, -1.0616, -0.7274, -0.4666],
[-0.5376, 1.4548, -0.3597, 0.0560]])
运算
按元素运算:标准算术运算符(+、-、*、/和**)已被重载过,被升级为按(对应)元素运算
⼴播机制:对不同形状的张量,将其复制元素形成形状相同的张量,再按元素运算
a = torch.arange(3).reshape((3, 1))
b = torch.arange(2).reshape((1, 2))
print(a, '\n', b)
tensor([[0],
[1],
[2]])
tensor([[0, 1]])
执行a+b运算时
print(a+b)
把a从 3 × 1 3\times 1 3×1的列向量向右复制一列,变成成 3 × 2 3\times 2 3×2的矩阵
tensor([[0, 0],
[1, 1],
[2, 2]])
把b从 1 × 2 1\times 2 1×2的行向量复制向下两行,变成成 3 × 2 3\times 2 3×2的矩阵
tensor([[0, 1],
[0, 1],
[0, 1]])
再按元素相加:
tensor([[0, 1],
[1, 2],
[2, 3]])
torch.cat()函数把两个张量连接起来:
X = torch.arange(12, dtype=torch.float32)