亲宝软件园·资讯

展开

Tensorflow中的矩阵运算函数 对Tensorflow中的矩阵运算函数详解

昆仑-郑教主 人气:0

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.diag函数相反,返回对角阵的对角元素

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足迹,即为对角值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的维度顺序,按照列表perm的维度排列调换tensor的顺序

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,b是否当作系数矩阵进行运算

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)))
#输出结果为[[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决定计算前是否进行转置

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分解,即为将一个对称正定矩阵表示成一个下三角矩阵L和其转置的乘积德分解

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]]

以上这篇对Tensorflow中的矩阵运算函数详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论