张量-矩阵操作函数

tf.diag(diagonal,name = None),该函数返回一个给定对角值得对角tensor。

示例代码如下:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

diagonal = tf.constant([2,3,4,5])

with tf.Session() as sess:
    print(sess.run(tf.diag(diagonal)))

tf.diag_part(input,name = None)该函数与tf.diag函数相反,返回对角阵得对角元素。

示例代码如下:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

diagonal = tf.constant([[1,0,0,0],[0,2,0,0],[0,0,3,0],[0,0,0,4]])

with tf.Session() as sess:
    print(sess.run(tf.diag_part(diagonal)))

tf.trace(x,name = None),该函数用于求一个2维tensor足迹,即对角值diagonal之和。

示例代码如下:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

diagonal = tf.constant([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])

with tf.Session() as sess:
    print(sess.run(tf.trace(diagonal)))
    #1+6+11+16=34

tf.transpose(a,perm = None,name = 'transpose'),该函数用于让输入的a按照参数perm指定的维度顺序进行转置操作。如果不设置perm,默认是一个全转置。

示例代码如下:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

diagonal = tf.constant([[1,2,3,4],[5,6,7,8]])

with tf.Session() as sess:
    print(sess.run(tf.transpose(diagonal)))

tf.reverse(tensor,dims,name = None),该函数用于将输入的张量沿着指定的维度进行反转。其中,dims是个列表,指向输入的张量的形状的索引。

示例代码如下:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

diagonal = tf.constant([[[[1,2,3,4],
                        [5,6,7,8],
                        [9,10,11,12]],
                        [[13,14,15,16],
                         [17,18,19,20],
                         [21,22,23,24]]]])

with tf.Session() as sess:
    print(sess.run(tf.shape(diagonal)))
    print("--------------------------------------")
    print(sess.run(tf.reverse(diagonal,[0])))
    print("--------------------------------------")
    print(sess.run(tf.reverse(diagonal,[1])))
    print("--------------------------------------")
    print(sess.run(tf.reverse(diagonal,[2])))
    print("--------------------------------------")
    print(sess.run(tf.reverse(diagonal,[3])))

张量-矩阵操作函数_第1张图片

tf.matmul(a,b,transpose_a = False,transpose_b = False,adjoint_a = False,adjoint_b = False,a_is_sparse = False,b_is_sparse = False,name = None),该函数用于计算矩阵相乘,也就是将矩阵a乘以矩阵b,生成a*b。

示例代码如下:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

a = tf.constant([[1,0],[0,3]])
b = tf.constant([[2,1],[0,2]])

with tf.Session() as sess:
    print(sess.run(tf.matmul(a,b)))

tf.matrix_determinant(input,name = None),该函数用来返回方阵的行列式。

示例代码如下:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

a = tf.constant([[1,2],[3,4]],dtype = tf.float32)

with tf.Session() as sess:
    print(sess.run(tf.matrix_determinant(a)))
    #1*4-2*3 = -2

tf.matrix_inverse(input,adjoint = None,name = None),该函数用于求方阵的逆矩阵。adjoint为True时,计算输入共轭矩阵的逆矩阵。(逆矩阵的定义:假设A和B都是n阶矩阵,如果AB=BA=E,则称方阵A可逆,并称方阵B是A的逆矩阵)

示例代码如下:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

a = tf.constant([[1,2],[3,4]],dtype = tf.float64)

with tf.Session() as sess:
    print(sess.run(tf.matrix_inverse(a)))

tf.cholesky(input,name = None),该函数对输入方阵进行cholesky分解,即为把一个对称正定矩阵表示成一个下三角矩阵L和其转置的乘积的分解。

示例代码如下:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

a = tf.constant([[1,0],[0,2]],dtype = tf.float64)

with tf.Session() as sess:
    print(sess.run(tf.cholesky(a)))

tf.matrix_solve(matrix,rhs,adjoint = None,name = None),该函数用于求解矩阵方程,返回矩阵变量。其中matrix为矩阵变量的系数,rhs为矩阵方程的结果。

示例代码如下:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

a = tf.constant([[1,2],[3,4]],dtype = tf.float64)
b = tf.constant([[5],[6]],dtype = tf.float64)

with tf.Session() as sess:
    print(sess.run(tf.matrix_solve(a,b)))

你可能感兴趣的:(常见知识点,矩阵,线性代数,tensorflow,python,人工智能,深度学习)