张量的基本操作
1.降维
降维求和求均值
x = torch.arange(20,dtype=torch.float32).reshape(5,4)
x,x.sum(),x.mean()
x,x.sum(axis=0),x.sum(axis=1)
x,x.mean(axis=0),x.mean(axis=1)
非降维求和求均值
sum_x = x.sum(axis=1,keepdims = True)
sum_x
点积
x = torch.ones(4,dtype = torch.float32)
y = torch.ones(4,dtype = torch.float32)
x,y,torch.dot(x,y)
向量积
A = torch.arange(20,dtype = torch.float32).reshape(5,4)
B = torch.arange(4,dtype = torch.float32)
A,B,torch.mv(A,B)
矩阵乘法
C = torch.arange(20,dtype = torch.float32).reshape(4,5)
A,C,torch.mm(A,C)
向量的范数
- L1范数表示的是向量元素的绝对值之和。
- L2范数表示的是向量元素的平方和的平方根。
u = torch.tensor([3.0,-4.0])
torch.norm(u)
torch.abs(u).sum()