Linear Algebra Review

Matrices and Vectors

Matrices are 2-dimensional arrays.

example:

1.3 Linear Algebra Review - 图1

The above matrix has five rows and three columns, so it is a 5 × 3matrix.

1.3 Linear Algebra Review - 图2


A vector is a matrix with one column and many rows.

example:

1.3 Linear Algebra Review - 图3

The above vector has four rows, so it is a 4-dimensional vector.

1.3 Linear Algebra Review - 图4


Matrices are usually denoted by uppercase names while vectors are lowercase.

Addition and Scalar Multiplication

Addition and Scalar Multiplication are element-wise, so you simply add or subtract each corresponding element (To add or subtract two matrices, their dimensions must be the same.)

example:

1.3 Linear Algebra Review - 图5

1.3 Linear Algebra Review - 图6

Matrix Vector Multiplication

1.3 Linear Algebra Review - 图7

这是矩阵相乘的一个特例,in a word,**A B = C, 就是A的第i行和B的第j列依次相乘再相加得到C的(i, j)位置元素的值

example:

1.3 Linear Algebra Review - 图8

Matrix Matrix Multiplication

example:

1.3 Linear Algebra Review - 图9

结果的第i列是第一个矩阵和第二个矩阵的第i列相乘得到的

Properties(属性)

  1. Matrices are not commutative(不可交换的): A × B ≠ B × A

example:

1.3 Linear Algebra Review - 图10

  1. Matrices are associative(关联的): (A × B) × C = A × (B × C)

  2. If I is a identity matrix(单位矩阵) : A × I = I × A

Inverse and Transpose

Matrix inverse(矩阵的逆): If A is an m × m matrix(square matrix), and if it has an inverse A^{-1}(逆矩阵)

1.3 Linear Algebra Review - 图11%20%3D%20A%5E%7B-1%7DA%20%3D%20I%0A#card=math&code=A%28A%5E%7B-1%7D%29%20%3D%20A%5E%7B-1%7DA%20%3D%20I%0A)

Matrix Transpose(矩阵的转置):Let A be an m × n matrix, and let B = A^{T}

Then B is an n × m matrix, and B{ij} = A{ji}

Pytorch

  1. import torch

定义tensor

  1. # 自定义数据
  2. a = torch.tensor([[5,7,9],[8,8,6],[-1,-9,-88],[7,0,7]])
  3. print(a)
  1. tensor([[ 5, 7, 9],
  2. [ 8, 8, 6],
  3. [ -1, -9, -88],
  4. [ 7, 0, 7]])
# 随机生成0-1之间的数
b = torch.rand(4,3)
print(b)
tensor([[0.3134, 0.4812, 0.0067],
        [0.5301, 0.5591, 0.1892],
        [0.6000, 0.5937, 0.0937],
        [0.3741, 0.4851, 0.9509]])
# 设定元素全为0,dtype指定数据类型
c = torch.zeros(3, 8, dtype=torch.long)
# 设定元素全为0
d = torch.ones(4, 3)

加减法

result1 = a + b # 等价于torch.add(a, b, out=result)
print(result1)
tensor([[  5.3134,   7.4812,   9.0067],
        [  8.5301,   8.5591,   6.1892],
        [ -0.4000,  -8.4063, -87.9063],
        [  7.3741,   0.4851,   7.9509]])

乘除法

e = torch.rand(4, 1)
result2 = a * e
print(result2)
tensor([[   6.4139,    8.9795,   11.5450],
        [  47.8200,   47.8200,   35.8650],
        [  -1.3584,  -12.2259, -119.5426],
        [   7.3210,    0.0000,    7.3210]])

求逆矩阵

f = torch.rand(5, 5)
print(f)
result3 = torch.empty(5, 5)
torch.inverse(f, out=result3)
print(result3)
tensor([[0.8013, 0.0199, 0.8192, 0.0184, 0.6136],
        [0.2154, 0.7296, 0.5942, 0.1686, 0.5256],
        [0.0608, 0.9719, 0.7722, 0.0216, 0.3838],
        [0.1829, 0.6466, 0.6206, 0.8242, 0.0798],
        [0.5894, 0.6581, 0.7713, 0.4012, 0.2679]])
tensor([[-0.7766,  0.6743, -1.5575, -1.6231,  3.1706],
        [-1.4639,  1.1553, -0.3732, -1.1495,  1.9633],
        [ 1.6820, -3.1634,  2.6344,  1.4405, -1.8495],
        [ 0.0111,  1.0091, -1.2064,  1.3720, -0.6854],
        [ 0.4452,  3.2752, -1.4351,  0.1923, -1.7141]])

转置

result4 = a.T
print(a)
print(result4)
tensor([[  5,   7,   9],
        [  8,   8,   6],
        [ -1,  -9, -88],
        [  7,   0,   7]])
tensor([[  5,   8,  -1,   7],
        [  7,   8,  -9,   0],
        [  9,   6, -88,   7]])