tensorflow2.0 Series (2): tensor calculation

Tensor calculation

tensorflow defines the basic tensor calculation lot, due to the special properties tensor, which arithmetic operations are mainly two types, one is based on the determinant / linear algebra, one is the calculation of the elemental-wise. The most common are:

Summation: tf.add
multiplication: tf.matmul,
inverse operation: tf.linalg.inv

Most are based on the definition in the calculation of linear algebra tf.linalg module. https://tensorflow.google.cn/api_docs/python/tf/linalg

tf.linalg module

Tensorflow linear algebra module provides a wealth of functions available for calls. In addition to the basic linear algebra determinant calculation, for example:

Adjoint (tf.linalg.adjoint)
value of the determinant of (tf.linalg.det)
is converted to a diagonal matrix (tf.linalg.diag)
eigenvalue and eigenvectors (tf.linalg.eigh)
determinant inversion ( tf.linalg.inv)
matrix multiplication (tf.linalg.matmul)
matrix transpose (tf.linalg.matrix_transpose)
the QR decomposition (tf.linalg.qr)
the SVD Singular value decomposition (tf.linalg.svd)
the LU decomposition (tf. linalg.lu)
determinant trace (tf.linalg.trace)

There are a number of advanced matrix analysis functions, such as adjoint matrix, the matrix cycle and so on.
class LinearOperator: Base class Defining A [BATCH of] Linear operator [S].
class LinearOperatorAdjoint: LinearOperator Representing The adjoint of Another operator adjoint.
class LinearOperatorBlockDiag:. Combines One or More LinearOperators in to A Block Diagonal Matrix block diagonal matrix
class LinearOperatorCirculant: LinearOperator acting like a circulant matrix circulant matrix.
class LinearOperatorCirculant2D: LinearOperator acting like circulant matrix block A block-circulant matrices.
class LinearOperatorCirculant3D: LinearOperator acting like circulant matrix block A nested inline block circulant matrix.
class LinearOperatorComposition: One composes or more LinearOperators. determinant may implement some of the user-defined operation
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 module

Commonly used algebraic functions

In the math module, commonly used centralized algebra functions, e.g.

Absolute value (tf.math.abs)
of a given list of all tensor tensor elements do and element-wise accumulated (tf.accumulate_n)
trigonometric functions (tf.math.sin, tf.math.cos, tf. Math.tan)
inverse trigonometric functions (tf.math.acos, tf.math.asin, tf.math.atan)
hyperbolic functions (tf.math.sinh, tf.math.cosh, tf.math.tanh)
trans-bis Qu cosine function (tf.math.asinh, tf.math.acosh)
Bessel spline fitting (tf.math.bassel_i0, tf.bassel_i0e, tf.math.bessel_i1, tf.math.bassel_i1e )
rounding (tf.math .ceil, tf.math.floor)
operations carried out along a dimension (tf.math.reduce_any, tf.math.reduce_std, tf.math.reduce.mean, ... )

There are also a function of depth learning-related, such as:

Confusion matrix (tf.math.confusion_matrix)
tensor calculation certain data (tf.math.segment_ *)

tf.math.segment_*

Operation on the specified piece of data carried out tensor. There segment_max, segment_mean, segment_min, segment_prod, segment_sum. To segment_max example, where the direct use of tensorflow an example API documentation.

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)

get:

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

Intrinsic functions can be given segment_id, labe is calculated sequentially 0, 1, 2, the maximum value of the data segment, the image may show the calculation process is as follows:
Here Insert Picture Description

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)

get:

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

Here some slight understand. Indeed, where the tensor segment_id divided into two segments, [[1,2,3,4] [4,3,2,1]] is set, [5,6,7,8] is a group . After comparing each maximum value, the image of the "column" is drawing

Here Insert Picture Description

Supplemental reading:
circulant: https: //www.cnblogs.com/torsor/p/8848641.html

Published 111 original articles · won praise 118 · views 280 000 +

Guess you like

Origin blog.csdn.net/happyhorizion/article/details/103849473