手机网站图片锚链接怎么做,网络初始网站,锤子手机网站模板,美食网页设计与制作einsum详解
该函数用于对一组输入 Tensor 进行 Einstein 求和#xff0c;该函数目前仅适用于paddle的动态图。
Einstein 求和是一种采用 Einstein 标记法描述的 Tensor 求和#xff0c;输入单个或多个 Tensor#xff0c;输出单个 Tensor。 paddle.einsum(equation, *opera…einsum详解
该函数用于对一组输入 Tensor 进行 Einstein 求和该函数目前仅适用于paddle的动态图。
Einstein 求和是一种采用 Einstein 标记法描述的 Tensor 求和输入单个或多个 Tensor输出单个 Tensor。 paddle.einsum(equation, *operands)
参数
equation (str)求和标记operands (Tensor, [Tensor, …])输入 Tensor
返回
Tensor输出 Tensor
求和特例 单操作数 迹trace 对角元diagonal 转置transpose 求和sum 双操作数 内积dot 外积outer 广播乘积mul* 矩阵乘matmul 批量矩阵乘bmm 多操作数 广播乘积mul* 多矩阵乘A.matmul(B).matmul(C)
关于求和标记的约定 维度分量下标Tensor 的维度分量下标使用英文字母表示不区分大小写如’ijk’表示 Tensor 维度分量为 i,j,k 下标对应输入操作数维度下标以,分段按顺序 1-1 对应输入操作数 广播维度省略号…表示维度的广播分量例如i…j’表示首末分量除外的维度需进行广播对齐 自由标和哑标输入标记中仅出现一次的下标为自由标重复出现的下标为哑标哑标对应的维度分量将被规约消去 输出输出 Tensor 的维度分量既可由输入标记自动推导也可以用输出标记定制化 自动推导输出 广播维度分量位于维度向量高维位置自由标维度分量按字母顺序排序位于维度向量低纬位置哑标维度分量不输出 定制化输出 维度标记中-右侧为输出标记 若输出包含广播维度则输出标记需包含… 输出标记为空时对输出进行全量求和返回该标量 输出不能包含输入标记中未出现的下标 输出下标不可以重复出现 哑标出现在输出标记中则自动提升为自由标 输出标记中未出现的自由标被降为哑标
例子 ‘…ij, …jk’该标记中 i,k 为自由标j 为哑标输出维度’…ik’ ‘ij - i’i 为自由标j 为哑标 ‘…ij, …jk - …ijk’i,j,k 均为自由标 ‘…ij, …jk - ij’若输入 Tensor 中的广播维度不为空则该标记为无效标记
求和规则
Einsum 求和过程理论上等价于如下四步但实现中实际执行的步骤会有差异。 第一步维度对齐将所有标记按字母序排序按照标记顺序将输入 Tensor 逐一转置、补齐维度使得处理后的所有 Tensor 其维度标记保持一致 第二步广播乘积以维度下标为索引进行广播点乘 第三步维度规约将哑标对应的维度分量求和消除 第四步转置输出若存在输出标记则按标记进行转置否则按广播维度字母序自由标的顺序转置返回转之后的 Tensor 作为输出 关于 trace 和 diagonal 的标记约定待实现功能 在单个输入 Tensor 的标记中重复出现的下标称为对角标对角标对应的坐标轴需进行对角化操作如’i…i’表示需对首尾坐标轴进行对角化 若无输出标记或输出标记中不包含对角标则对角标对应维度规约为标量相应维度取消等价于 trace 操作 若输出标记中包含对角标则保留对角标维度等价于 diagonal 操作 实例实践
首先看一下一维度简单实验
import paddle# 定义两个输入矩阵
# paddle.seed(102)
# x paddle.rand([4])
# y paddle.rand([5])
x paddle.to_tensor([1,2,], dtypefloat32)
y paddle.to_tensor([3,4,5], dtypefloat32)# sum
sum_x paddle.einsum(i-, x).numpy()# dot
dox_x paddle.einsum(i,i-, x, x).numpy()# outer
outer_xy paddle.einsum(i,j-ij, x, y).numpy()print(fx: {x.numpy()}, shape: {x.shape})
print(fy: {y.numpy()}, shape: {y.shape})
print(fsum_x: {sum_x}, shape: {sum_x.shape})
print(fdox_x: {dox_x}, shape: {dox_x.shape})
print(fouter_xy: {outer_xy}, shape: {outer_xy.shape})结果输出为
x: [1. 2.], shape: [2]
y: [3. 4. 5.], shape: [3]
sum_x: 3.0, shape: ()
dox_x: 5.0, shape: ()
outer_xy: [[ 3. 4. 5.][ 6. 8. 10.]], shape: (2, 3)然后看一下高纬度的实验
import paddle# A paddle.rand([2, 3, 2])
# B paddle.rand([2, 2, 3])
A paddle.to_tensor([[[1,2],[1,2],[1,2]], [[1,2],[1,2],[1,2]]], dtypefloat32)
B paddle.to_tensor([[[3,4,5],[3,4,5]], [[3,4,5],[3,4,5]]], dtypefloat32)# transpose
transpose_A paddle.einsum(ijk-kji, A)# batch matrix multiplication
BMM_AB paddle.einsum(ijk, ikl-ijl, A,B)# Ellipsis transpose
ET_A paddle.einsum(...jk-...kj, A)# Ellipsis batch matrix multiplication
EBMM_AB paddle.einsum(...jk, ...kl-...jl, A,B)print(fA: {A.numpy()}, shape: {A.shape})
print(fB: {B.numpy()}, shape: {B.shape})
print(ftranspose_A: {transpose_A.numpy()}, shape: {transpose_A.shape})
print(fBMM_AB: {BMM_AB.numpy()}, shape: {BMM_AB.shape})
print(fET_A: {ET_A.numpy()}, shape: {ET_A.shape})
print(fEBMM_AB: {EBMM_AB.numpy()}, shape: {EBMM_AB.shape})结果输出为
A: [[[1. 2.][1. 2.][1. 2.]][[1. 2.][1. 2.][1. 2.]]], shape: [2, 3, 2]
B: [[[3. 4. 5.][3. 4. 5.]][[3. 4. 5.][3. 4. 5.]]], shape: [2, 2, 3]
transpose_A: [[[1. 1.][1. 1.][1. 1.]][[2. 2.][2. 2.][2. 2.]]], shape: [2, 3, 2]
BMM_AB: [[[ 9. 12. 15.][ 9. 12. 15.][ 9. 12. 15.]][[ 9. 12. 15.][ 9. 12. 15.][ 9. 12. 15.]]], shape: [2, 3, 3]
ET_A: [[[1. 1. 1.][2. 2. 2.]][[1. 1. 1.][2. 2. 2.]]], shape: [2, 2, 3]
EBMM_AB: [[[ 9. 12. 15.][ 9. 12. 15.][ 9. 12. 15.]][[ 9. 12. 15.][ 9. 12. 15.][ 9. 12. 15.]]], shape: [2, 3, 3]reference
关于matmul可以查看https://blog.csdn.net/orDream/article/details/133744368 官方链接 misc{BibEntry2023Oct, title {{einsum-API文档-PaddlePaddle深度学习平台}}, year {2023}, month oct, urldate {2023-10-10}, language {chinese}, note {[Online; accessed 10. Oct. 2023]}, url {https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/einsum_cn.html} }