1.对角矩阵的设置
a = torch.rand(2)
print(a)
x = torch.diag(a,diagonal=0)
print(x)
x = torch.diag(a,diagonal=1)
print(x)
x = torch.diag(a,diagonal=2)
print(x)
x = torch.diag(a,diagonal=-1)
print(x)
------------------------------------------------------------------------------
result:
tensor([0.6151, 0.7596])
tensor([[0.6151, 0.0000],
[0.0000, 0.7596]])
tensor([[0.0000, 0.6151, 0.0000],
[0.0000, 0.0000, 0.7596],
[0.0000, 0.0000, 0.0000]])
tensor([[0.0000, 0.0000, 0.6151, 0.0000],
[0.0000, 0.0000, 0.0000, 0.7596],
[0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000]])
tensor([[0.0000, 0.0000, 0.0000],
[0.6151, 0.0000, 0.0000],`在这里插入代码片`
[0.0000, 0.7596, 0.0000]])
2.矩阵的迹
x = torch.randn(2,4)
print(x)
print(torch.trace(x))
x = torch.randn(2,2)
print(x)
print(torch.trace(x))
------------------------------------------------------------------------------
result:
tensor([[ 0.0316, 0.8957, 0.6893, 1.3485],
[-1.7997, -0.6455, -0.3539, 0.0214]])
tensor(-0.6139)
tensor([[ 0.1226, 0.2207],
[-0.5384, 3.7622]])
tensor(3.8848)
3.上三角矩阵与下三角矩阵
x = torch.randn(2,5)
print(torch.tril(x))
print(torch.tril(x,diagonal=2))
print(torch.tril(x,diagonal=-1))
print(torch.triu(x))
print(torch.triu(x,diagonal=4))
print(torch.triu(x,diagonal=-1))
-----------------------------------------------------------------------------
result:
tensor([[-0.1253, 0.0000, 0.0000, 0.0000, 0.0000],
[-0.8299, 0.8951, 0.0000, 0.0000, 0.0000]])
tensor([[-0.1253, -0.8371, 0.2722, 0.0000, 0.0000],
[-0.8299, 0.8951, -0.0954, -1.2229, 0.0000]])
tensor([[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
[-0.8299, 0.0000, 0.0000, 0.0000, 0.0000]])
tensor([[-0.1253, -0.8371, 0.2722, 1.6253, 0.4773],
[ 0.0000, 0.8951, -0.0954, -1.2229, -1.5745]])
tensor([[0.0000, 0.0000, 0.0000, 0.0000, 0.4773],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000]])
tensor([[-0.1253, -0.8371, 0.2722, 1.6253, 0.4773],
[-0.8299, 0.8951, -0.0954, -1.2229, -1.5745]])
4.矩阵的乘积mm和bmm
x = torch.Tensor([[[1,2,3],[4,5,6]]])
print(x.shape)
y = torch.Tensor([[[9],[8],[7]]])
print(y.shape)
print(torch.bmm(x,y),torch.bmm(x,y).squeeze(0).squeeze(1))
print(torch.mm(x.squeeze(0),y.squeeze(0)))
x = torch.Tensor([1,2,3,4])
y = torch.Tensor([4,3,2,1])`在这里插入代码片`
print(torch.dot(x,y))
-----------------------------------------------------------------------------
result:
torch.Size([1, 2, 3])
torch.Size([1, 3, 1])
tensor([[[ 46.],
[118.]]]) tensor([ 46., 118.])
tensor([[ 46.],
[118.]])
tensor(20.)
5.矩阵的相乘再相加
x = torch.Tensor([[1,2]])
print(x.shape)
batch1 = torch.Tensor([[1,2,3]])
print(batch1.shape)
batch2 = torch.Tensor([[1,2],[3,4],[5,6]])
print(batch2.shape)
print(torch.addmm(x,batch1,batch2,beta=0.1,alpha=5))
-----------------------------------------------------------------------------
result:
torch.Size([1, 2])
torch.Size([1, 3])
torch.Size([3, 2])
tensor([[110.1000, 140.2000]])
6.批量矩阵相乘再相加
x = torch.Tensor([[1,2]])
print(x.shape)
batch1 = torch.Tensor([[[1,2,3]]])
print(batch1.shape)
batch2 = torch.Tensor([[[1,2],[3,4],[5,6]]])
print(batch2.shape)
print(torch.addbmm(x,batch1,batch2,beta=0.1,alpha=10))
7.矩阵乘向量再相加
x = torch.Tensor([1,2,3])
print(x.shape)
mat = torch.Tensor([[1],[2],[3]])
print(mat.shape)
vec = torch.Tensor([3])
print(vec.shape)
print(torch.addmv(x,mat,vec,beta=1,alpha=1))
-----------------------------------------------------------------------------
result:
torch.Size([3])
torch.Size([3, 1])
torch.Size([1])
tensor([ 4., 8., 12.])
8.特征值及特征向量
x = torch.Tensor([[9,2,3],[4,5,8],[7,10,9]])
print(torch.eig(x))
print(torch.eig(x,eigenvectors=True))
-----------------------------------------------------------------------------
result:
torch.return_types.eig(
eigenvalues=tensor([[19.0221, 0.0000],
[ 6.1871, 0.0000],
[-2.2092, 0.0000]]),
eigenvectors=tensor([]))
torch.return_types.eig(
eigenvalues=tensor([[19.0221, 0.0000],
[ 6.1871, 0.0000],
[-2.2092, 0.0000]]),
eigenvectors=tensor([[ 0.3385, 0.7846, -0.0528],
[ 0.5373, -0.4213, -0.7286],
[ 0.7725, -0.4548, 0.6829]]))