tensorflow2.0系列(2):张量的运算

张量运算

tensorflow定义了很多张量的基本运算,由于张量的特殊属性,其运算操作主要有两类,一类是基于行列式/线性代数的,一类是对elemental-wise的运算。最常用的有:

求和:tf.add
乘法:tf.matmul,
求逆运算:tf.linalg.inv

基于线性代数的运算大多数都在tf.linalg模块中定义。https://tensorflow.google.cn/api_docs/python/tf/linalg

tf.linalg 模块

tensorflow 的线性代数模块提供了非常丰富的函数可供调用。除了基本的线性代数行列式运算,例如:

伴随矩阵(tf.linalg.adjoint)
行列式的值(tf.linalg.det)
转化为对角阵(tf.linalg.diag)
求解特征值和特征向量(tf.linalg.eigh )
行列式求逆(tf.linalg.inv)
矩阵乘法(tf.linalg.matmul)
矩阵转置(tf.linalg.matrix_transpose)
QR分解(tf.linalg.qr)
SVD奇异值分解(tf.linalg.svd)
LU分解(tf.linalg.lu)
行列式的迹(tf.linalg.trace)

还有高级一些的矩阵分析中函数,例如伴随矩阵,循环矩阵等等。
class LinearOperator: Base class defining a [batch of] linear operator[s].
class LinearOperatorAdjoint: LinearOperator representing the adjoint of another operator. 伴随矩阵
class LinearOperatorBlockDiag: Combines one or more LinearOperators in to a Block Diagonal matrix. 分块对角阵
class LinearOperatorCirculant: LinearOperator acting like a circulant matrix. 循环矩阵
class LinearOperatorCirculant2D: LinearOperator acting like a block circulant matrix. 分块循环矩阵
class LinearOperatorCirculant3D: LinearOperator acting like a nested block circulant matrix. 内嵌分块循环矩阵
class LinearOperatorComposition: Composes one or more LinearOperators. 可以实现一些用户自定义的行列式操作
class LinearOperatorDiag: LinearOperator acting like a [batch] square diagonal matrix. 求batch的对角方阵
class LinearOperatorFullMatrix: LinearOperator that wraps a [batch] matrix.
class LinearOperatorHouseholder: LinearOperator acting like a [batch] of Householder transformations.
class LinearOperatorIdentity: LinearOperator acting like a [batch] square identity matrix.
class LinearOperatorInversion: LinearOperator representing the inverse of another operator.
class LinearOperatorKronecker: Kronecker product between two LinearOperators.Kronecker乘积
class LinearOperatorLowRankUpdate: Perturb a LinearOperator with a rank K update.
class LinearOperatorLowerTriangular: LinearOperator acting like a [batch] square lower triangular matrix.
class LinearOperatorScaledIdentity: LinearOperator acting like a scaled [batch] identity matrix A = c I.
class LinearOperatorToeplitz: LinearOperator acting like a [batch] of toeplitz matrices.
class LinearOperatorZeros: LinearOperator acting like a [batch] zero matrix.

tf.math模块

常用的代数函数

在math模块,集中了常用的代数运算函数,例如

求绝对值( tf.math.abs )
对给定的tensor列表中所有tensor的元素做element-wise的累加和(tf.accumulate_n)
三角函数(tf.math.sin, tf.math.cos, tf.math.tan)
反三角函数(tf.math.acos, tf.math.asin, tf.math.atan)
双曲函数(tf.math.sinh, tf.math.cosh, tf.math.tanh)
反双曲余弦函数(tf.math.asinh, tf.math.acosh)
Bessel样条拟合(tf.math.bassel_i0, tf.bassel_i0e, tf.math.bessel_i1, tf.math.bassel_i1e)
取整(tf.math.ceil, tf.math.floor)
沿某个维度进行的运算(tf.math.reduce_any, tf.math.reduce_std, tf.math.reduce.mean, …)

也有一些深度学习相关的函数,例如:

混淆矩阵(tf.math.confusion_matrix)
张量某段数据的运算(tf.math.segment_* )

tf.math.segment_*

对tensor的指定的一段数据进行的操作。有segment_max, segment_mean, segment_min, segment_prod, segment_sum。以segment_max为例,这里直接用tensorflow API文档中的一个例子。

segment_ids=tf.constant([0,0,0,1,2,2,3,3])
v=tf.Variable([5,1,7,2,3,4,1,3])
m=tf.math.segment_max(v,segment_ids)

得到:

m
Out[112]: <tf.Tensor: id=901, shape=(4,), dtype=int32, numpy=array([7, 2, 4, 3], dtype=int32)>

函数内部用给定的segment_id, 依次计算了labe是0, 1,2,3的片段中数据的最大值,计算过程可以形象地展示如下:
在这里插入图片描述

c = tf.constant([[1,2,3,4], [4, 3, 2, 1], [5,6,7,8]])
segmax_c = tf.math.segment_max(c, tf.constant([0, 0, 1]))
print(segmax_c)

得到:

tf.Tensor(
[[4 3 3 4]
 [5 6 7 8]], shape=(2, 4), dtype=int32)

这里略有一些费解。实际上,这里的segment_id将tensor分为了两个片段,[[1,2,3,4] [4,3,2,1]] 为一组, [5,6,7,8]为一组。之后在比较每个“列”上的最大值,形象地画图为

在这里插入图片描述

补充阅读:
循环行列式:https://www.cnblogs.com/torsor/p/8848641.html

发布了111 篇原创文章 · 获赞 118 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/happyhorizion/article/details/103849473