How Eigen tensor do Batch Matrix Multiplication?
Pytorch, numpy and tensorflow can do batch matrix multiplication just as follows:
How can I use Eigen tensor do matrix multiplication? I test the following code by Eigen tensor.
#include <iostream>
#include "/eigen-3.4.0/Eigen/Core"
#include "/eigen-3.4.0/Eigen/Dense"
#include "/eigen-3.4.0/unsupported/Eigen/CXX11/Tensor"
using namespace std;
int main()
{
Eigen::Tensor<float, 3> a(3, 2, 5);
a.setRandom();
Eigen::Tensor<float, 3> b(3, 5, 3);
b.setRandom();
Eigen::array<Eigen::IndexPair<int>, 1> contraction_pair = {Eigen::IndexPair<int>(2, 1)};
Eigen::Tensor<float, 4> c = a.contract(b, contraction_pair);
const auto& d = c.dimensions();
cout << "Dim Size is " << c.size() << " dim 0: " << d[0] << " dim 1: " << d[1] << " dim 2: " << d[2] << " dim 3: " << d[3] << endl;
return 0;
}
the code result is as following: Dim Size is 54 dim 0: 3 dim 1: 2 dim 2: 3 dim 3: 3 Therefore, the c is 4 dimension(3, 2, 3, 3). However, the batch matrix multiplication result is 3 dimension(3, 2, 3). How can I do this by Eigen tensor?Thanks a lot.