【PyTorch学习笔记】6:Broadcasting,对Tensor的合并与拆分,Tensor运算

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kwame211/article/details/88013731

Broadcasting
Broadcasting也就和之前学MPI时候学的广播一样,能够实现自动维度扩展,有点像上节学的expand的功能,但是是自动完成的,而且不需要像repeat那样对数据进行拷贝,可以节省内存。

从最后面的维度开始匹配。
在前面插入若干维度。
将维度的size从1通过expand变到和某个Tensor相同的维度。
总之,Broadcasting也就是自动实现了若干unsqueeze和expand操作,以使两个Tensor的shape一致,从而完成某些操作(往往是加法)。

例如[batch,channel,height,width]=[4,3,32,32] [batch,channel,height,width]=[4,3,32,32][batch,channel,height,width]=[4,3,32,32]:如果加上一个[32,32] [32,32][32,32]的Tensor,意思就是不管是4张图片中的哪一张,也不管是什么通道,都加上一个32*32的Feature Map;如果加上一个[3,1,1] [3,1,1][3,1,1],意思就是对3个通道分别加上一个值;如果加上一个[1,1,1,1] [1,1,1,1][1,1,1,1],这和加上一个[1] [1][1]一样,也就是对每个元素都加上一个相同的Bais值。

对Tensor的合并
维度合并(cat)
要保证其它维度的size是相同的。

import torch

a = torch.rand(4, 32, 8)
b = torch.rand(5, 32, 8)
print(torch.cat([a, b], dim=0).shape)
 
运行结果:

torch.Size([9, 32, 8]
 
合并新增(stack)
stack需要保证两个Tensor的shape是一致的,这就像是有两类东西,它们的其它属性都是一样的(比如男的一张表,女的一张表)。使用stack时候要指定一个维度位置,在那个位置前会插入一个新的维度,因为是两类东西合并过来所以这个新的维度size是2,通过指定这个维度是0或者1来选择性别是男还是女。

c = torch.rand(4, 3, 32, 32)
d = torch.rand(4, 3, 32, 32)
print(torch.stack([c, d], dim=2).shape)
print(torch.stack([c, d], dim=0).shape)
 
运行结果:

torch.Size([4, 3, 2, 32, 32])
torch.Size([2, 4, 3, 32, 32])
 
对Tensor的拆分
按照size的长度拆分(split)
对一个Tensor而言,要拆分的那个维度的size就是"这个维度的总长度"了,可以指定拆分后的几个Tensor各取多少长度,或者指定每个Tensor取多少长度。

import torch

a = torch.rand(2, 4, 3, 32, 32)
a1, a2 = a.split(1, dim=0)  # 对0号维度拆分,拆分后每个Tensor取长度1
print(a1.shape, a2.shape)

b = torch.rand(4, 3, 32, 32)
b1, b2 = b.split([2, 1], dim=1)  # 对1号维度拆分,拆分后第一个维度取2,第二个维度取1
print(b1.shape, b2.shape)
 
运行结果:

torch.Size([1, 4, 3, 32, 32]) torch.Size([1, 4, 3, 32, 32])
torch.Size([4, 2, 32, 32]) torch.Size([4, 1, 32, 32])
 
按照份数等量拆分(chunk)
给定在指定的维度上要拆分得的份数,就会按照指定的份数尽量等量地进行拆分。

c = torch.rand(7, 4)
c1, c2, c3, c4 = c.chunk(4, dim=0)
print(c1.shape, c2.shape, c3.shape, c4.shape)
 
运行结果:

torch.Size([2, 4]) torch.Size([2, 4]) torch.Size([2, 4]) torch.Size([1, 4])
 
Tensor运算
相加
import torch

# 这两个Tensor加减乘除会对b自动进行Broadcasting
a = torch.rand(3, 4)
b = torch.rand(4)

c1 = a + b
c2 = torch.add(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
 
运行结果:

torch.Size([3, 4]) torch.Size([3, 4])
tensor(1, dtype=torch.uint8)
 
相减
a = torch.rand(3, 4)
b = torch.rand(4)

c1 = a - b
c2 = torch.sub(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
 
运行结果:同上

哈达玛积(element wise)
c1 = a * b
c2 = torch.mul(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
 
运行结果:同上

相除
c1 = a / b
c2 = torch.div(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
 
运行结果:同上

矩阵乘法(matrix mul)
使用Torch.mm(只能用于2d的Tensor)或者Torch.matmul(和符号@效果一样)。

dim=2
import torch

a = torch.ones(2, 1)
b = torch.ones(1, 2)
print(torch.mm(a, b).shape)
print(torch.matmul(a, b).shape)
print((a @ b).shape)
 
运行结果:

torch.Size([2, 2])
torch.Size([2, 2])
torch.Size([2, 2])
 
dim>2
对于高维的Tensor,定义其矩阵乘法仅在最后的两个维度上,要求前面的维度必须保持一致,就像矩阵的索引一样。

c = torch.rand(4, 3, 28, 64)
d = torch.rand(4, 3, 64, 32)
print(torch.matmul(c, d).shape)
 
运行结果:

torch.Size([4, 3, 28, 32])
1
注意,在这种情形下的矩阵相乘,前面的"矩阵索引维度"如果符合Broadcasting机制,也会自动做广播,然后相乘。

c = torch.rand(4, 3, 28, 64)
d = torch.rand(4, 1, 64, 32)
print(torch.matmul(c, d).shape)
 
运行结果:

torch.Size([4, 3, 28, 32])
1
幂和开方
import torch

a = torch.full([2, 2], 3)

b = a.pow(2)  # 也可以a**2
print(b)

c = b.sqrt()  # 也可以a**(0.5)
print(c)

d = b.rsqrt()  # 平方根的倒数
print(d)
 
运行结果:

tensor([[9., 9.],
        [9., 9.]])
tensor([[3., 3.],
        [3., 3.]])
tensor([[0.3333, 0.3333],
        [0.3333, 0.3333]])
 
指数和取对数
注意log是以自然对数为底数的,以2为底的用log2,以10为底的用log10。

import torch

a = torch.exp(torch.ones(2, 2))  # 得到2*2的全是e的Tensor
print(a)
print(torch.log(a))  # 取自然对数
 
运行结果:

tensor([[2.7183, 2.7183],
        [2.7183, 2.7183]])
tensor([[1., 1.],
        [1., 1.]])
 
近似值操作
import torch

a = torch.tensor(3.14)
print(a.floor(), a.ceil(), a.trunc(), a.frac())  # 取下,取上,取整数,取小数
b = torch.tensor(3.49)
c = torch.tensor(3.5)
print(b.round(), c.round())  # 四舍五入
 
运行结果:

tensor(3.) tensor(4.) tensor(3.) tensor(0.1400)
tensor(3.) tensor(4.)
1
2
裁剪
即对Tensor中的元素进行范围过滤,不符合条件的可以把它变换到范围内部(边界)上,常用于梯度裁剪(gradient clipping),即在发生梯度离散或者梯度爆炸时对梯度的处理。

实际使用时可以查看梯度的(L2范数)模来看看需不需要做处理:w.grad.norm(2)。

import torch

grad = torch.rand(2, 3) * 15  # 0~15随机生成
print(grad.max(), grad.min(), grad.median())  # 最大值最小值平均值

print(grad)
print(grad.clamp(10))  # 最小是10,小于10的都变成10
print(grad.clamp(3, 10))  # 最小是3,小于3的都变成3;最大是10,大于10的都变成10
 
运行结果:

tensor(14.7400) tensor(1.8522) tensor(10.5734)
tensor([[ 1.8522, 14.7400,  8.2445],
        [13.5520, 10.5734, 12.9756]])
tensor([[10.0000, 14.7400, 10.0000],
        [13.5520, 10.5734, 12.9756]])
tensor([[ 3.0000, 10.0000,  8.2445],
        [10.0000, 10.0000, 10.0000]])
--------------------- 
作者:刘知昊 
来源:CSDN 
原文:https://blog.csdn.net/SHU15121856/article/details/87886885 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/kwame211/article/details/88013731