tensorflow2.0系列(2):张量的运算

2023-05-16

目录

  • 张量运算
    • tf.linalg 模块
    • tf.math模块
      • 常用的代数函数
      • tf.math.segment_*

张量运算

tensorflow定义了很多张量的基本运算,由于张量的特殊属性,其运算操作主要有两类,一类是基于行列式/线性代数的,一类是对elemental-wise的运算。最常用的有:

求和:tf.add
乘法:tf.matmul,
求逆运算:tf.linalg.inv

基于线性代数的运算大多数都在tf.linalg模块中定义。https://tensorflow.google.cn/api_docs/python/tf/linalg

tf.linalg 模块

tensorflow 的线性代数模块提供了非常丰富的函数可供调用。除了基本的线性代数行列式运算,例如:

伴随矩阵(tf.linalg.adjoint)
行列式的值(tf.linalg.det)
转化为对角阵(tf.linalg.diag)
求解特征值和特征向量(tf.linalg.eigh )
行列式求逆(tf.linalg.inv)
矩阵乘法(tf.linalg.matmul)
矩阵转置(tf.linalg.matrix_transpose)
QR分解(tf.linalg.qr)
SVD奇异值分解(tf.linalg.svd)
LU分解(tf.linalg.lu)
行列式的迹(tf.linalg.trace)

还有高级一些的矩阵分析中函数,例如伴随矩阵,循环矩阵等等。
class LinearOperator: Base class defining a [batch of] linear operator[s].
class LinearOperatorAdjoint: LinearOperator representing the adjoint of another operator. 伴随矩阵
class LinearOperatorBlockDiag: Combines one or more LinearOperators in to a Block Diagonal matrix. 分块对角阵
class LinearOperatorCirculant: LinearOperator acting like a circulant matrix. 循环矩阵
class LinearOperatorCirculant2D: LinearOperator acting like a block circulant matrix. 分块循环矩阵
class LinearOperatorCirculant3D: LinearOperator acting like a nested block circulant matrix. 内嵌分块循环矩阵
class LinearOperatorComposition: Composes one or more LinearOperators. 可以实现一些用户自定义的行列式操作
class LinearOperatorDiag: LinearOperator acting like a [batch] square diagonal matrix. 求batch的对角方阵
class LinearOperatorFullMatrix: LinearOperator that wraps a [batch] matrix.
class LinearOperatorHouseholder: LinearOperator acting like a [batch] of Householder transformations.
class LinearOperatorIdentity: LinearOperator acting like a [batch] square identity matrix.
class LinearOperatorInversion: LinearOperator representing the inverse of another operator.
class LinearOperatorKronecker: Kronecker product between two LinearOperators.Kronecker乘积
class LinearOperatorLowRankUpdate: Perturb a LinearOperator with a rank K update.
class LinearOperatorLowerTriangular: LinearOperator acting like a [batch] square lower triangular matrix.
class LinearOperatorScaledIdentity: LinearOperator acting like a scaled [batch] identity matrix A = c I.
class LinearOperatorToeplitz: LinearOperator acting like a [batch] of toeplitz matrices.
class LinearOperatorZeros: LinearOperator acting like a [batch] zero matrix.

tf.math模块

常用的代数函数

在math模块,集中了常用的代数运算函数,例如

求绝对值( tf.math.abs )
对给定的tensor列表中所有tensor的元素做element-wise的累加和(tf.accumulate_n)
三角函数(tf.math.sin, tf.math.cos, tf.math.tan)
反三角函数(tf.math.acos, tf.math.asin, tf.math.atan)
双曲函数(tf.math.sinh, tf.math.cosh, tf.math.tanh)
反双曲余弦函数(tf.math.asinh, tf.math.acosh)
Bessel样条拟合(tf.math.bassel_i0, tf.bassel_i0e, tf.math.bessel_i1, tf.math.bassel_i1e)
取整(tf.math.ceil, tf.math.floor)
沿某个维度进行的运算(tf.math.reduce_any, tf.math.reduce_std, tf.math.reduce.mean, …)

也有一些深度学习相关的函数,例如:

混淆矩阵(tf.math.confusion_matrix)
张量某段数据的运算(tf.math.segment_* )

tf.math.segment_*

对tensor的指定的一段数据进行的操作。有segment_max, segment_mean, segment_min, segment_prod, segment_sum。以segment_max为例,这里直接用tensorflow API文档中的一个例子。

segment_ids=tf.constant([0,0,0,1,2,2,3,3])
v=tf.Variable([5,1,7,2,3,4,1,3])
m=tf.math.segment_max(v,segment_ids)

得到:

m
Out[112]: <tf.Tensor: id=901, shape=(4,), dtype=int32, numpy=array([7, 2, 4, 3], dtype=int32)>

函数内部用给定的segment_id, 依次计算了labe是0, 1,2,3的片段中数据的最大值,计算过程可以形象地展示如下:
在这里插入图片描述

c = tf.constant([[1,2,3,4], [4, 3, 2, 1], [5,6,7,8]])
segmax_c = tf.math.segment_max(c, tf.constant([0, 0, 1]))
print(segmax_c)

得到:

tf.Tensor(
[[4 3 3 4]
 [5 6 7 8]], shape=(2, 4), dtype=int32)

这里略有一些费解。实际上,这里的segment_id将tensor分为了两个片段,[[1,2,3,4] [4,3,2,1]] 为一组, [5,6,7,8]为一组。之后在比较每个“列”上的最大值,形象地画图为

在这里插入图片描述

补充阅读:
循环行列式:https://www.cnblogs.com/torsor/p/8848641.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

tensorflow2.0系列(2):张量的运算 的相关文章

随机推荐

  • win11启用旧右键菜单(不折叠)的方案,亲测有效

    我电脑的winodws11版本如下 在以上win版本下 xff0c 测试网上说的以下几个办法都不行 1 权利与暗访时在终端运行 reg exe delete HKCU Software Classes CLSID 86ca1aa0 34aa
  • Android中的动画总结

    Android 动画 三种总结 xff1a 属性动画 xff1a 动态的改变属性产生动画效果 xff1b 改变动画的属性 xff0c 两个重要的类 xff1a 1 ValueAnimator 类是先改变值 xff0c 然后 手动赋值 给对象
  • ubuntu自动登录tty终端的最简方法

    背景 在嵌入式系统经常需要自动登录tty xff0c 以实现业务程序开机启动的效果 网上有篇文章ubuntu自动登录tty1 shell text 配置转发挺多 xff0c 但我弄明白原理后 xff0c 觉得可以进一步简化 xff0c 经测
  • git为指定项目设置用户名密码

    我们如果没有为项目设置用户名密码 xff0c 那么每次提交都会有提示账号和密码输入 xff1a 1 找到你项目的半隐藏文件 git文件夹 xff0c 通常只要git init 就会生成这样一个文件夹 xff0c 现在双击进入文件夹 xff1
  • JAVA 访问windows共享文件夹

    一 使用技术 JCIFS框架对Windows共享文件夹进行读写 xff0c 使用了SMB通信协议 xff0c 它为局域网内不同的计算机之间提供文件和打印机等资源共享服务 二 共享文件夹设置 测试共享文件夹机器windows版本为win10家
  • 前端代码调试:Webstorm调试js

    前言 目前前端开发 JavaScript的debug一般都是用chrome和firefox的开发者工具进行调试 xff0c 浏览器工具使用不方便 xff0c webstorm支持了在代码上打断点 xff0c 在编辑器里debug js代码
  • CV:基本概念和工具

    文章目录 计算机视觉像素 xff0c 颜色 xff0c 通道 xff0c 图像和颜色空间的概念图像文件后缀有损压缩和无损压缩 Python的计算机视觉库PIL xff1a python 图像处理类库载入PIL库基本操作命令 openCVop
  • OCR技术概览

    OCR技术概览 OCR Optical Character Recognition 光学字符识别技术主要分为手写体识别和印刷体识别两类 印刷体识别比手写体识别要简单 因为印刷体更规范 字体来自于计算机字库 尽管印刷过程中可能会发生不清晰 粘
  • CV:图像色彩空间及色彩处理

    文章目录 基本概念RGB空间HSV空间HSL空间 色彩变换灰度变换色彩反向 调整像素区间增强对比度直方图均衡化 图像平滑 减少噪声图像平均高斯滤波 图像梯度sobel算子 scharr算子prewitt算子Laplacian 算子 参考及更
  • OpenCV角点检测: Harris算子, ShiTomasi算子

    角点检测 角点的特征检测与匹配是Computer Vision 应用总重要的一部分 xff0c 这需要寻找图像之间的特征建立对应关系 点 xff0c 也就是图像中的特殊位置 xff0c 是很常用的一类特征 xff0c 点的局部特征也可以叫做
  • 集成学习:让算法和算法赛跑

    文章目录 集成学习的基本概念构建弱分类器 xff1a 决策树自助采样法bootstrappingbootstrapping的核心思想bootstrapping与permutation的区别 baggingboosting为何bagging会
  • backbone模型:FCN、SRN、STN

    文章目录 FCN网络CNN图像分割模型结构FCN github资源FCN的优缺点 SRN 网络什么是空间规整 spatial regularization xff09 SRN网络github资源 STN网络空间变换器localisation
  • docker部署机器学习/深度学习模型的容器化方案

    文章目录 什么是dockerdocker的优点 docker image镜像Dockerfile 文件Dockerfile配置例子 创建docker镜像 docker container 容器模型部署参考和更多阅读 docker部署机器学习
  • RNN模型训练经验总结

    文章目录 RNN模型训练经验总结数据准备 look at your data 小步试错 搭建模型设置端到端的训练评估框架 forward propagation设置激活函数dropout back propagation设置学习率 lear
  • 算法的时间复杂度和空间复杂度

    如何评价算法的性能 定义 一个算法中的语句执行次数称为 语句频度 或 时间频度 约定 检验算法的效率 xff0c 主要考虑 最坏时间复杂度 和 平均时间复杂度 一般不特别说明 xff0c 讨论的时间复杂度均是最坏情况下的时间复杂度 时间复杂
  • 【LeetCode】LCS最长公共子序列

    最长公共子序列 题目描述思路分析递归结构算法实现输出最长子序列算法实现 题目描述 思路分析 设A 61 a0 xff0c a1 xff0c xff0c am xff0c B 61 b0 xff0c b1 xff0c xff0c bn xff
  • 升级到tensorflow2.0

    目录 从tensorflow1 x升级到2 x方案一 xff1a 依然使用tf 1 x的脚本方案二 xff1a 升级项目代码到2 x tensorflow2 0推出以后 xff0c 全面拥抱keras xff0c 简化了API接口 xff0
  • tensorflow2.0系列(1):张量

    目录 tensor xff1a 张量张量的数据类型Dtype类函数tf as type xff1a 定义Dtype类型对象tf dtypes cast xff1a 将张量映射 投射为新的类型tf dtypes complex xff1a 将
  • tensorflow 2.0系列(3):eager execution和计算图

    目录 tf 2 0 的新特性 xff1a eager execution开启eager模式关闭eager模式 构建图默认图创建多个计算图启动会话 从公式到计算图绘制心形曲线开根号线性回归 tf 2 0 的新特性 xff1a eager ex
  • tensorflow2.0系列(2):张量的运算

    目录 张量运算tf linalg 模块tf math模块常用的代数函数tf math segment 张量运算 tensorflow定义了很多张量的基本运算 xff0c 由于张量的特殊属性 xff0c 其运算操作主要有两类 xff0c 一类