2016-11-27 128 views
0

在本徵,可以使用做很容易張量收縮:如何在特徵中做張量的外積?

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×10和3×2,並且限定尺寸的新張量的C 5×10×3×2,使得

C_ijkl = A_ij * B_kl 

我可以很容易地,如果需要寫這樣的方法,但我得到的意義上它會如果我使用更加優化本地特徵方法。如果您使用本地方法,我也希望能夠使用GPU支持,這對eigen來說非常簡單。

感謝。

+0

你試過了''運營商? –

+0

@Kerrek這不起作用,請看這裏:http://stackoverflow.com/questions/41098944/why-does-the-following-fail-with-eigen – kloop

回答

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

}