# [译]深度学习中所需的线性代数知识

### 标量

``````# 内置标量
a = 5
b = 7.5
print(type(a))
print(type(b))
print(a + b)
print(a - b)
print(a * b)
print(a / b)

``````<class 'int'>
<class 'float'>
12.5
-2.5
37.5
0.6666666666666666

``````import numpy as np

# 判断是否为标量的函数
def isscalar(num):
if isinstance(num, generic):
return True
else:
return False

print(np.isscalar(3.1))
print(np.isscalar([3.1]))
print(np.isscalar(False))

``````True
False
True

### 向量

``````import numpy as np

# 定义向量

x = [1, 2, 3]
y = [4, 5, 6]

print(type(x))

# 这样做不会得到向量和
print(x + y)

# 使用 Numpy 进行向量相加

print(z)
print(type(z))

# 向量叉乘
mul = np.cross(x, y)
print(mul)

``````<class 'list'>
[1, 2, 3, 4, 5, 6]
[5 7 9]
<class 'numpy.ndarray'>
[-3  6 -3]

### 矩阵

\$python

``````>>> import numpy as np
>>> x = np.matrix([[1,2],[2,3]])
>>> x
matrix([[1, 2],
[2, 3]])

>>> a = x.mean(0)
>>> a
matrix([[1.5, 2.5]])
>>> # 对矩阵求均值。（其中 axis 不设置值，对 m*n 个数求均值，返回一个实数；axis = 0：压缩行，对各列求均值，返回 1* n 矩阵；axis =1 ：压缩列，对各行求均值，返回 m *1 矩阵）。
>>> z = x.mean(1)
>>> z
matrix([[1.5],
[2.5]])
>>> z.shape
(2, 1)
>>> y = x - z
matrix([[-0.5,  0.5],
[-0.5,  0.5]])
>>> print(type(z))
<class 'numpy.matrixlib.defmatrix.matrix'>

#### 矩阵加法

``````# 矩阵加法

import numpy as np

x = np.matrix([[1, 2], [4, 3]])

sum = x.sum()
print(sum)
# Output: 10

#### 矩阵与矩阵相加

C = A + B (**A 与 B 的维度需要相同 **)

`shape` 方法返回矩阵的维度，`add` 方法接受两个矩阵参数并返回这两个矩阵的和。如果两个矩阵的维度不一致 `add` 方法将会抛出一个异常，说无法将其相加。

``````# 矩阵与矩阵相加

import numpy as np

x = np.matrix([[1, 2], [4, 3]])
y = np.matrix([[3, 4], [3, 10]])

print(x.shape)
# (2, 2)
print(y.shape)
# (2, 2)

print(m_sum)
print(m_sum.shape)
"""
Output :
[[4  6]
[7 13]]
(2, 2)
"""

#### 矩阵与标量相加

``````# 矩阵与标量相加

import numpy as np

x = np.matrix([[1, 2], [4, 3]])
s_sum = x + 1
print(s_sum)
"""
Output:
[[2 3]
[5 4]]
"""

#### 矩阵与标量的乘法

``````# 矩阵与标量的乘法

import numpy as np

x = np.matrix([[1, 2], [4, 3]])
s_mul = x * 3
print(s_mul)
"""
[[3  6]
[12  9]]
"""

#### 矩阵乘法

``````# 矩阵乘法

import numpy as np

a = [[1, 0], [0, 1]]
b = [1, 2]
np.matmul(a, b)
# Output: array([1, 2])

complex_mul = np.matmul([2j, 3j], [2j, 3j])
print(complex_mul)
# Output: (-13+0j)

#### 矩阵转置

A=[a_ij_]mxn

AT=[a_ji_]n×m

``````
# 矩阵转置

import numpy as np

a = np.array([[1, 2], [3, 4]])
print(a)
"""
[[1 2]
[3 4]]
"""
a.transpose()
print(a)
"""
array([[1, 3],
[2, 4]])
"""

### 张量

``````
import torch

a = torch.Tensor([26])

print(type(a))
# <class 'torch.FloatTensor'>

print(a.shape)
# torch.Size([1])

# 创建一个 5*3 的随机 torch 变量。
t = torch.Tensor(5, 3)
print(t)
"""
0.0000e+00  0.0000e+00  0.0000e+00
0.0000e+00  7.0065e-45  1.1614e-41
0.0000e+00  2.2369e+08  0.0000e+00
0.0000e+00  0.0000e+00  0.0000e+00
nan         nan -1.4469e+35
[torch.FloatTensor of size 5x3]
"""
print(t.shape)
# torch.Size([5, 3])

Python 中张量的运算操作：

``````import torch

# 创建张量

p = torch.Tensor(4,4)
q = torch.Tensor(4,4)
ones = torch.ones(4,4)

print(p, q, ones)
"""
Output:
0.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00
1.6009e-19  4.4721e+21  6.2625e+22  4.7428e+30
3.1921e-09  8.0221e+17  5.1019e-08  8.1121e+17
8.1631e-07  8.2022e+17  1.1703e-19  1.5637e-01
[torch.FloatTensor of size 4x4]

0.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00
1.8217e-44  1.1614e-41  0.0000e+00  2.2369e+08
0.0000e+00  0.0000e+00  2.0376e-40  2.0376e-40
nan         nan -5.3105e+37         nan
[torch.FloatTensor of size 4x4]

1  1  1  1
1  1  1  1
1  1  1  1
1  1  1  1
[torch.FloatTensor of size 4x4]
"""

print("Subtraction:{}".format(p - ones))
print("Multiplication:{}".format(p * ones))
print("Division:{}".format(q / ones))

"""
0.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00
1.6009e-19  4.4721e+21  6.2625e+22  4.7428e+30
3.1921e-09  8.0221e+17  5.1019e-08  8.1121e+17
nan         nan -5.3105e+37         nan
[torch.FloatTensor of size 4x4]
Subtraction:
-1.0000e+00 -1.0000e+00 -1.0000e+00 -1.0000e+00
-1.0000e+00  4.4721e+21  6.2625e+22  4.7428e+30
-1.0000e+00  8.0221e+17 -1.0000e+00  8.1121e+17
-1.0000e+00  8.2022e+17 -1.0000e+00 -8.4363e-01
[torch.FloatTensor of size 4x4]
Multiplication:
0.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00
1.6009e-19  4.4721e+21  6.2625e+22  4.7428e+30
3.1921e-09  8.0221e+17  5.1019e-08  8.1121e+17
8.1631e-07  8.2022e+17  1.1703e-19  1.5637e-01
[torch.FloatTensor of size 4x4]
Division:
0.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00
1.8217e-44  1.1614e-41  0.0000e+00  2.2369e+08
0.0000e+00  0.0000e+00  2.0376e-40  2.0376e-40
nan         nan -5.3105e+37         nan
[torch.FloatTensor of size 4x4]
"""