如何在特征中转置Tensor

时间:2016-10-02 10:04:59

标签: eigen

我正在尝试获得两个张量的矩阵乘积,其中一个张量应该在乘以(At*B)之前进行转置。

到目前为止,我在eigen documentation中找到的是没有任何转置且矩阵转置的矩阵乘积。

我正在寻找一种方法来直接收缩两个张量,其中一个张量被转置,或者通过在收缩之前转置一个张量。

2 个答案:

答案 0 :(得分:3)

我想通了,转换效果可以用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);

答案 1 :(得分: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);
相关问题