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

yipeiwu_com6年前Python基础

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中的矩阵运算函数详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

在Python的Django框架中更新数据库数据的方法

先使用一些关键参数创建对象实例,如下: >>> p = Publisher(name='Apress', ... address='2855 Telegra...

Python读取系统文件夹内所有文件并统计数量的方法

大家先看一下Python os模块中的部分函数 python 路径相关的函数 os.listdir(dirname):列出dirname下的目录和文件 os.getcwd():获得当前...

python 删除指定时间间隔之前的文件实例

遍历指定文件夹下的文件,根据文件后缀名,获取指定类型的文件列表;根据文件列表里的文件路径,逐个获取文件属性里的“修改时间”,如果“修改时间”与“系统当前时间”差值大于某个值,则删除该文件...

深入了解Python中pop和remove的使用方法

Python关于删除list中的某个元素,一般有两种方法,pop()和remove()。 remove() 函数用于移除列表中某个值的第一个匹配项。 remove()方法语法: list...

说说如何遍历Python列表的方法示例

说说如何遍历Python列表的方法示例

如果需要对列表中的每个元素执行相同操作,这时就需要遍历列表的所有元素。 books=['半生缘','往事并不如烟','心是孤独的猎手'] for book in books: p...