如何在Eigen中转置Tensor

4
我想要计算两个张量的矩阵积,其中一个张量在相乘之前应该被转置 (At*B)。
目前我在Eigen文档中发现了没有任何转置和两个矩阵都进行转置的矩阵积。
我正在寻找一种方法,在其中一个张量被转置后直接收缩两个张量,或在压缩之前转置一个张量。
2个回答

6

我明白了,利用shuffle方法可以实现转置效果。

Eigen::Tensor<int, 2> m(3, 5);
m.setValues(
{
    {1, 2, 3, 4, 5},
    {6, 7, 8, 9, 10},
    {11, 12, 13, 14, 15}
});

Eigen::array<int, 2> shuffling({1, 0});

Eigen::Tensor<int, 2> transposed = m.shuffle(shuffling);
Eigen::Tensor<int, 2> original = transposed.shuffle(shuffling);

3
您也可以直接使用缩写:
Eigen::Tensor<int, 2> A(3, 5);
Eigen::Tensor<int, 2> B(3, 5);
Eigen::array<int, 1> contraction_indices;
// This will contract the first dimension of A with the first dim of B,
// effectively computing At*B
contraction_indices[0] = {0, 0};
Eigen::Tensor<int, 2> Result = A.contract(B, contraction_indices);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接