张量征服,我如何选择应收缩的索引

Tensor-contraction, how do i choose the indexes which should be contracted over?

本文关键字:索引 选择 征服 何选择 张量      更新时间:2023-10-16

我想用eigen :: tensor模块执行简单的张量合同,但是到目前为止,我不理解您对正确的尺寸进行调整的方式。

这是我当前的代码:

Eigen::Tensor<double, 3> B(3,4,3); B.setRandom();
Eigen::Tensor<double, 3> C(3,4,3); C.setRandom();
// Eigen::array<Eigen::IndexList<int,int,int>,1> idx = 
//                      {Eigen::IndexList<int,int,int>(1,0,0)}; 
//                       also does not seem to be the way
Eigen::array<int,3> idx({0,0,1});
Eigen::Tensor<double, 4> D = B.contract(C, idx);

我只想在 b 的最后一个维度和 c 的第一个维度上收缩。但是我不明白系统的工作原理,并且文档仅为您提供了2D量的示例。

//the first element of IDXPair is the choosen index in B and the second the idx for C
Eigen::array<Eigen::IndexPair<int>,1> idx = {Eigen::IndexPair<int>(2,0)};

在这里,第二个索引将乘以第二个张量的零。

indexpair准确地说明了:张量二措施中的第一个索引映射到第二个张量的第二个索引。

idxpair(a,b)=> a(1,2,3,4,x) * b(x,5,6,7,8,9),其中a是最后一个维度的索引cas x和b第二张张量

中的尺寸索引