2016-10-02 110 views
1

我試圖獲得兩張張量的矩陣乘積,其中張量之一應該在乘以之前轉置(At*B)。如何在特徵中轉置張量

到目前爲止,我在eigen documentation中發現的是沒有任何轉置且兩個矩陣都轉置的矩陣乘積。

我正在尋找一種方法來直接收縮張量中的一個張量,或者通過在收縮它之前轉置一個張量。

回答

2

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

您也可以直接使用收縮:

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);