Matrix arithmetic function in TensorFlow

Source: Internet
Author: User
Tags arithmetic constant

Tf.diag (Diagonal,name=none) #生成对角矩阵

Import Tensorflowas TF;
diagonal=[1,1,1,1] with
TF. Session () as Sess:
     print (Sess.run (Tf.diag (diagonal)))
#输出的结果为 [[1 0 0 0] [0 1 0 0] [0 0 1 0
              ]              [0 0 0 1]]
Tf.diag_part (Input,name=none) #功能与tf. The Diag function, in contrast, returns the diagonal element of the diagonal array

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)))
#输出结果为 [1,1,1,1]

Tf.trace (X,name=none) #求一个2维Tensor足迹, 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))) #输出结果为4

Tf.transpose (a,perm=none,name= ' transpose ') #调换tensor的维度顺序 the order in which the tensor is swapped by the dimension of the 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)))   #输出结果为 [[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) #矩阵相乘

Transpose_a=false,transpose_b=false #运算前是否转置

A_is_sparse=false,b_is_sparse=false #a, whether B is calculated 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 (b)))
#输出结果为 [[2 1]           [0 6]]
Tf.matrix_determinant (Input,name=none) #计算行列式

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)))   
#输出结果为3.0
Tf.matrix_inverse (Input,adjoint=none,name=none)

Adjoint deciding whether to transpose before calculation

Import TensorFlow as TF;
A =tf.constant ([1,0,0,2],shape=[2,2],dtype=tf.float64) with
TF. Session () as Sess:
    print (Sess.run (Tf.matrix_inverse (A)))
#输出结果为 [[1.   0.]           [0. 0.5]]

Tf.cholesky (Input,name=none) #对输入方阵cholesky分解, that is, to represent a symmetric positive definite matrix as a lower triangular matrix L and its transpose multiply jide decomposition

Import TensorFlow as TF;
A =tf.constant ([1,0,0,2],shape=[2,2],dtype=tf.float64) with
TF. Session () as Sess:
    print (Sess.run (Tf.cholesky (A)))
#输出结果为 [[1.          0.]           [0. 1.41421356]]












Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.