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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章