PyTorch 笔记(09)— Tensor 线性代数计算(torch.trace、torch.diag、torch.mm、torch.dot、torch.inverse逆矩阵、转置)

1. 常用函数

常用线性表函数如下表所示:
PyTorch 笔记(09)— Tensor 线性代数计算(torch.trace、torch.diag、torch.mm、torch.dot、torch.inverse逆矩阵、转置)_第1张图片

2. 使用示例

2.1 torch.trace

In [22]: import torch as t

In [23]: a = t.arange(1, 10).view(3,3)

In [24]: a
Out[24]: 
tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])

In [25]: a.trace()
Out[25]: tensor(15)

2.2 torch.diag

In [24]: a
Out[24]: 
tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])

In [26]: a.diag()
Out[26]: tensor([1, 5, 9])

In [27]: a.diag(diagonal=1)
Out[27]: tensor([2, 6])

In [28]: a.diag(diagonal=2)
Out[28]: tensor([3])

2.3 torch.t

In [24]: a
Out[24]: 
tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])

In [30]: a.t()
Out[30]: 
tensor([[1, 4, 7],
        [2, 5, 8],
        [3, 6, 9]])

In [31]: 

2.4 torch.inverse

注意:并不是所有的矩阵都可逆。对不可逆矩阵进行求逆会报错。

RuntimeError: "inverse_cpu" not implemented for 'Long'
In [37]: z = t.Tensor([[0,1,2], [1,1,4],[2,-1,0]])

In [38]: z
Out[38]: 
tensor([[ 0.,  1.,  2.],
        [ 1.,  1.,  4.],
        [ 2., -1.,  0.]])

In [39]: z.inverse()
Out[39]: 
tensor([[ 2.0000, -1.0000,  1.0000],
        [ 4.0000, -2.0000,  1.0000],
        [-1.5000,  1.0000, -0.5000]])

In [40]: 

2.5 torch.triu

In [40]: a
Out[40]: 
tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])

In [41]: a.triu()
Out[41]: 
tensor([[1, 2, 3],
        [0, 5, 6],
        [0, 0, 9]])

In [43]: a.triu(1)
Out[43]: 
tensor([[0, 2, 3],
        [0, 0, 6],
        [0, 0, 0]])

In [44]: a.triu(2)
Out[44]: 
tensor([[0, 0, 3],
        [0, 0, 0],
        [0, 0, 0]])

In [45]: 

2.6 torch.mm

In [46]: a = t.arange(1, 5).view(2,2)

In [47]: a
Out[47]: 
tensor([[1, 2],
        [3, 4]])

In [48]: b = t.arange(2, 6).view(2,2)

In [49]: b
Out[49]: 
tensor([[2, 3],
        [4, 5]])

In [50]: a.mm(b)
Out[50]: 
tensor([[10, 13],
        [22, 29]])

In [51]: 

2.6 torch.dot

In [62]: torch.dot(torch.tensor([2, 3]), torch.tensor([2, 1]))
Out[62]: tensor(7)

In [56]: a
Out[56]: 
tensor([[1, 2],
        [3, 4]])

In [57]: b
Out[57]: 
tensor([[2, 3],
        [4, 5]])

In [58]: a.dot(b)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-58-ac6884f5cff1> in <module>
----> 1 a.dot(b)

RuntimeError: 1D tensors expected, got 2D, 2D tensors at C:\w\b\windows\pytorch\aten\src\TH/generic/THTensorEvenMoreMath.cpp:431

这个好像与 NumPydot 不太一样

In [65]: a = np.array([[1,2], [3,4]])

In [66]: a
Out[66]: 
array([[1, 2],
       [3, 4]])

In [67]: b = np.array([[2,3], [4,5]])

In [68]: b
Out[68]: 
array([[2, 3],
       [4, 5]])

In [69]: a.dot(b)
Out[69]: 
array([[10, 13],
       [22, 29]])

In [70]: np.dot(a,b)
Out[70]: 
array([[10, 13],
       [22, 29]])

In [71]: 

你可能感兴趣的:(PyTorch)