张量(Tensor),是Tensorflow里常用的一个数据结构
张量有很严谨的定义,可暂且把它简单理解为一个多维空间,一维张量就是向量,二维就是矩阵,三维直接就叫三维张量,Tensorflow就是围绕张量运算的一个库。——助教老师
LU
初识LU
Tensorflow的线性代数函数tf.linalg.lu
data = tf.constant([2, 1, 8, 7], dtype=tf.float32, shape=[2,2])
tf.linalg.lu(data)
output
Lu(lu=<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[ 8. , 7. ],
[ 0.25, -0.75]], dtype=float32)>, p=<tf.Tensor: shape=(2,), dtype=int32, numpy=array([1, 0])>)
tensorflow官方详解
tf.linalg.lu
Computes the LU decomposition of one or more square matrices. The input is a tensor of shape […, M, M] whose inner-most 2 dimensions form square matrices. The input has to be invertible. The output consists of two tensors LU and P containing the LU decomposition of all input submatrices […, :, :]. LU encodes the lower triangular and upper triangular factors. For each input submatrix of shape [M, M], L is a lower triangular matrix of shape [M, M] with unit diagonal whose entries correspond to the strictly lower triangular part of LU. U is a upper triangular matrix of shape [M, M] whose entries correspond to the upper triangular part, including the diagonal, of LU. P represents a permutation matrix encoded as a list of indices each between 0 and M-1, inclusive. If P_mat denotes the permutation matrix corresponding to P, then the L, U and P satisfies P_mat * input = L * U.
输入限制
- 最内部的两维需构成方阵(square matrix)
- 可逆(invertible)
输出
- LU矩阵:由L矩阵的下三角元素(不包括对角线),和U矩阵的上三角元素(包括对角线)组合而成
- L(lower triangular),L矩阵的对角线是单位对角线(unit diagonal)
- U(upper triangular)
- P矩阵:置换矩阵(permutation matrix),取值为下标index
- 置换矩阵参考
- 置换矩阵由单位矩阵的行列交换而形成,用于构成矩阵的行列交换运算,可用于矩阵交换行后化简为上三角矩阵U
LU分解
LU分解可用于求解线性方程 Ax=b ,主要思路:
- 令
A
=
L
U
A=LU
A=LU,代入方程得
L
U
x
=
b
LUx=b
LUx=b,看作
L
y
=
b
Ly=b
Ly=b 后,求得
y
y
y
- 由
y
=
U
x
y=Ux
y=Ux,最终求得
x
x
x
相当于表示一种把A化为上三角矩阵U的方式,即左乘
L
?
1
L^{-1}
L?1,
L
?
1
A
=
U
L^{-1}A=U
L?1A=U LU分解求解参考,解不一定唯一
SVD
初识SVD分解
data = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
tf.linalg.svd(data)
output
(<tf.Tensor: shape=(2,), dtype=float32, numpy=array([5.4649854 , 0.36596614], dtype=float32)>,
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-0.4045536 , -0.91451436],
[-0.91451436, 0.40455353]], dtype=float32)>,
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-0.5760485 , 0.81741554],
[-0.81741554, -0.5760485 ]], dtype=float32)>)
奇异值分解(Singular Value Decomposition)
回顾特征值分解(EVD, eigen value decomposition):
-
A
A
A 为实对称矩阵(方阵)
-
A
=
Q
Λ
Q
T
A=Q\Lambda Q^T
A=QΛQT,其中
Λ
\Lambda
Λ 是对角矩阵,
Q
Q
Q 是由特征向量构成的标准正交阵
EVD可用于降维,但对矩阵要求较高,SVD把EVD的概念拓展到普通矩阵上 SVD求解参考
(有待完善)
|