The matrix operation function in Tensorflow is explained in detail

  • 2020-11-26 18:49:59
  • OfStack

tf.diag(diagonal,name=None) # generates diagonal matrix


import tensorflowas tf;
diagonal=[1,1,1,1]
with tf.Session() as sess:
  print(sess.run(tf.diag(diagonal))) 

 # The output is [[1 0 0 0]
    [0 1 0 0]
    [0 0 1 0]
    [0 0 0 1]]

tf.diag_part (input,name=None) # functions as opposed to the tf.diag function, returning diagonal elements of a diagonal matrix


import tensorflow as tf;
diagonal =tf.constant([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]])
with tf.Session() as sess:
 print(sess.run(tf.diag_part(diagonal)))

# The output result is [1,1,1,1]

tf.trace(x,name=None) # find 1 2d Tensor footprint, which is the sum of the diagonal values diagonal


import tensorflow as tf;
diagonal =tf.constant([[1,0,0,3],[0,1,2,0],[0,1,1,0],[1,0,0,1]])
with tf.Session() as sess:
 print(sess.run(tf.trace(diagonal)))# The output result is 4

tf.transpose(a,perm=None,name='transpose') # swap the tensor dimension order, swapping the tensor order according to the dimensions of list perm


import tensorflow as tf;
diagonal =tf.constant([[1,0,0,3],[0,1,2,0],[0,1,1,0],[1,0,0,1]])
with tf.Session() as sess:
 print(sess.run(tf.transpose(diagonal))) # The output result is [[1 0 0 1]
                             [0 1 1 0]
                             [0 2 1 0]
                             [3 0 0 1]]

tf.matmul (a,b,transpose_a=False,transpose_b=False,a_is_sparse=False,b_is_sparse=False,name=None) # matrix multiplication

transpose_a=False,transpose_b=False # whether to transpose before operation

a_is_sparse=False,b_is_sparse=False #a, whether b operates as a coefficient matrix


import tensorflow as tf;
A =tf.constant([1,0,0,3],shape=[2,2])
B =tf.constant([2,1,0,2],shape=[2,2])
with tf.Session() as sess:
 print(sess.run(tf.matmul(A,B)))

# The output result is [[2 1]
   [0 6]]

matrix_determinant(input,name=None) # to calculate the determinant


import tensorflow as tf;
A =tf.constant([1,0,0,3],shape=[2,2],dtype=tf.float32)
with tf.Session() as sess:
 print(sess.run(tf.matrix_determinant(A))) 

# The output result is 3.0

tf.matrix_inverse(input,adjoint=None,name=None)

adjoint decides whether to transpose before calculating


 # The output is [[1 0 0 0]
    [0 1 0 0]
    [0 0 1 0]
    [0 0 0 1]]
0

 # The output is [[1 0 0 0]
    [0 1 0 0]
    [0 0 1 0]
    [0 0 0 1]]
1

The decomposition of input square matrix cholesky (input,name=None) # is the multiplicative and cumulative decomposition of a symmetric positive definite matrix to a lower three-angle matrix L and its transpose


 # The output is [[1 0 0 0]
    [0 1 0 0]
    [0 0 1 0]
    [0 0 0 1]]
2

# The output result is [[ 1.   0.  ]
   [ 0.   1.41421356]]

Related articles: