欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

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

时间:2021-08-23 09:26:19|栏目: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]]

上一篇:Python实现统计英文文章词频的方法分析

栏    目:Python代码

下一篇:Python面向对象之Web静态服务器

本文标题:对Tensorflow中的矩阵运算函数详解

本文地址:http://www.codeinn.net/misctech/169254.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有