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


Posted in Python onJuly 27, 2018

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自动扫雷实现方法
Jul 25 Python
python访问mysql数据库的实现方法(2则示例)
Jan 06 Python
Python实现类似比特币的加密货币区块链的创建与交易实例
Mar 20 Python
Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
Sep 14 Python
python scipy求解非线性方程的方法(fsolve/root)
Nov 12 Python
Python3.6实现带有简单界面的有道翻译小程序
Apr 16 Python
超实用的 30 段 Python 案例
Oct 10 Python
python 实现从高分辨图像上抠取图像块
Jan 02 Python
对python中 math模块下 atan 和 atan2的区别详解
Jan 17 Python
Python在终端通过pip安装好包以后在Pycharm中依然无法使用的问题(三种解决方案)
Mar 10 Python
Python urllib.request对象案例解析
May 11 Python
降低python版本的操作方法
Sep 11 Python
python+influxdb+shell编写区域网络状况表
Jul 27 #Python
TensorFlow 合并/连接数组的方法
Jul 27 #Python
pytorch训练imagenet分类的方法
Jul 27 #Python
使用pytorch进行图像的顺序读取方法
Jul 27 #Python
PyTorch读取Cifar数据集并显示图片的实例讲解
Jul 27 #Python
pytorch 数据集图片显示方法
Jul 26 #Python
mac安装pytorch及系统的numpy更新方法
Jul 26 #Python
You might like
PHP3 safe_mode 失效漏洞
2006/10/09 PHP
利用ThinkPHP内置的ThinkAjax实现异步传输技术的实现方法
2011/12/19 PHP
php数组编码转换示例详解
2014/03/11 PHP
php表单请求获得数据求和示例
2014/05/15 PHP
PHP数组遍历知识汇总(包含遍历方法、数组指针操作函数、数组遍历测速)
2014/07/05 PHP
php使用GD2绘制几何图形示例
2017/02/15 PHP
PHP中让json_encode不自动转义斜杠“/”的方法
2017/02/28 PHP
重定向实现代码
2006/11/20 Javascript
javascript dom 操作详解 js加强
2009/07/13 Javascript
基于jquery的button默认enter事件(回车事件)。
2011/05/18 Javascript
Js nodeType 属性全面解析
2013/11/14 Javascript
jQuery使用height()获取高度需要注意的地方
2014/12/13 Javascript
js实现鼠标经过时图片滚动停止的方法
2015/02/16 Javascript
jQuery使用append在html元素后同时添加多项内容的方法
2015/03/26 Javascript
JavaScript中匿名函数的用法及优缺点详解
2016/06/01 Javascript
微信小程序实现运动步数排行功能(可删除)
2018/07/05 Javascript
vue+element模态框中新增模态框和删除功能
2019/06/11 Javascript
vue分页器组件编写方法详解
2019/06/28 Javascript
Python 利用内置set函数对字符串和列表进行去重的方法
2018/06/29 Python
Django框架使用富文本编辑器Uedit的方法分析
2018/07/31 Python
Python生态圈图像格式转换问题(推荐)
2019/12/02 Python
Django视图、传参和forms验证操作
2020/07/15 Python
python中判断文件结束符的具体方法
2020/08/04 Python
Shopee越南:东南亚与台湾电商平台
2019/02/03 全球购物
西班牙土拨鼠床垫公司,感觉在云端:Marmota
2019/03/18 全球购物
Bugatchi官方网站:男士服装在线
2019/04/10 全球购物
ruby如何进行集成操作?Ruby能进行多重继承吗?
2013/10/16 面试题
家具促销活动方案
2014/02/16 职场文书
工程师岗位职责规定
2014/02/26 职场文书
企业精细化管理实施方案
2014/03/23 职场文书
关于孝道的演讲稿
2014/05/21 职场文书
公证书格式
2015/01/23 职场文书
项目建议书
2015/02/04 职场文书
幼儿园安全管理制度
2015/08/05 职场文书
创业计划书之网络外卖
2019/10/31 职场文书
python调用ffmpeg命令行工具便捷操作视频示例实现过程
2021/11/01 Python