如何在Eigen中进行张量的外积?

时间:2016-11-27 14:13:55

标签: c++ eigen

在本征中,使用以下方法可以很容易地进行张力收缩:

Tensor<double, 1> tensor1;
Tensor<double, 2> tensor2;

// fill with data so that
// tensor1 is of dimensions [10] and tensor2 of dimensions [5,10]

std::array<Eigen::IndexPair<int>, 1> product_dims1 = { IndexPair<int>(1, 0) };

auto tensor = tensor2.contract(tensor1, product_dims1);

// now tensor is of dimensions [5]

我正在寻找一种与收缩相反的方法,这意味着需要两个张量A和B,比如尺寸5 x 10和3 x 2,并定义了一个尺寸为5 x 10 x 3 x 2的新张量C这样

  C_ijkl = A_ij * B_kl

如果有必要,我可以很容易地编写这样的方法,但是如果我使用本机特征方法,我会感觉它会更加优化。我还希望能够使用GPU支持,如果你使用本机方法,这对于eigen非常容易。

感谢。

2 个答案:

答案 0 :(得分:1)

解决方案可能过于简单:您必须在没有索引的情况下签订合同

Eigen::array<Eigen::IndexPair<long>,0> empty_index_list = {};
Tensor<double, 2> A_ij(4, 4);
Tensor<double, 2> B_kl(4, 4);
Tensor<double, 4> C_ijkl = A_ij.contract(B_kl, empty_index_list);

答案 1 :(得分:0)

您可以通过重新整形输入张量来获得外部产品,并使用额外的一维尺寸填充尺寸,然后在新尺寸上进行广播。

对于两个等级2和一个等级4的张量,你有C_ijkl = A_ij * B_kl它看起来像:

#include <Eigen/Core>
#include <unsupported/Eigen/CXX11/Tensor>

using namespace Eigen;

int main() {

Tensor<double, 2> A_ij(4, 4);
Tensor<double, 2> B_kl(4, 4);
Tensor<double, 4> C_ijkl(4, 4, 4, 4);

Tensor<double, 4>::Dimensions A_pad(4, 4, 1, 1);
array<int, 4> A_bcast(1, 1, 4, 4);

Tensor<double, 4>::Dimensions B_pad(1, 1, 4, 4);
array<int, 4> B_bcast(4, 4, 1, 1);

C_ijkl = A_ij.reshape(A_pad).broadcast(A_bcast) * 
         B_kl.reshape(B_pad).broadcast(B_bcast);

}
相关问题