标量:仅包含一个数值被称为标量
向量:向量可以被视为标量值组成的列表
矩阵:正如向量将标量从零阶推广到一阶,矩阵将向量从一阶推广到二阶。
- A = torch.arange(20).reshape(5, 4)
- A.T //转置
张量:是描述具有任意数量轴的n维数组的通用方法
X = torch.arange(24).reshape(2, 3, 4)
张量与张量
- A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
- B = A.clone() # 通过分配新内存,将A的一个副本分配给B
- #A, A + B,A*B //按元素相加 按元素相乘(Hadamard积)
张量与标量
- a = 2
- X = torch.arange(24).reshape(2, 3, 4)
- a + X, (a * X).shape
- A.shape, A.sum()
- A_sum_axis0 = A.sum(axis=0)
- A.mean()// A.sum() / A.numel()
给定两个向量

torch.dot(x, y) //相当于torch.sum(x * y)

torch.cross(x, y)
矩阵和向量相乘,使用`mv`函数,注意,`A`的列维数(沿轴1的长度)必须与`x`的维数(其长度)相同。
torch.mv(A, x)
两个矩阵
和
torch.mm(A, B)
L1范数:

torch.abs(u).sum()
L2范数:
- u = torch.tensor([3.0, -4.0])
- torch.norm(u)
L_p范数:

Frobenius范数:

torch.norm(torch.ones((4, 9)))