Search code examples
c++tensorfloweigeneigen3

Convert an Eigen::TensorMap to Eigen::Tensor


It's possible to convert an Eigen::Map to a Matrix by assignment:

vector<float> v = { 1, 2, 3, 4 };
auto m_map = Eigen::Map<Eigen::Matrix<float, 2, 2, Eigen::RowMajor>>(&v[0]);
Eigen::MatrixXf m = m_map;
cout << m << endl;

This produces:

 1 2
 3 4

If I try to do something similar with a Tensor:

vector<float> v = { 1, 2, 3, 4 };
auto mapped_t = Eigen::TensorMap<Eigen::Tensor<float, 2, Eigen::RowMajor>>(&v[0], 2, 2);
Eigen::Tensor<float, 2> t = mapped_t;

I simply get the compiler error YOU_MADE_A_PROGRAMMING_MISTAKE. Is there any way to convert a TensorMap to a Tensor?


Solution

  • Well, Eigen::RowMajor is not the default for Eigen::Tensor which means you are not assigning to the same type which means YOU_MADE_A_PROGRAMMING_MISTAKE. You have to explicitly request swapping the layout.

    #include <vector>
    #include <unsupported/Eigen/CXX11/Tensor>
    
    int main()
    {
      std::vector<float> v = { 1, 2, 3, 4 };
      auto mapped_t = Eigen::TensorMap<Eigen::Tensor<float, 2, Eigen::RowMajor>>(&v[0], 2, 2);
      Eigen::Tensor<float, 2> t = Eigen::TensorLayoutSwapOp<Eigen::Tensor<float, 2, Eigen::RowMajor>>(mapped_t);
    }
    

    Using C++14 you could write a nice instantiator function for that.

    #include <type_traits>
    #include <vector>
    #include <unsupported/Eigen/CXX11/Tensor>
    
    namespace Eigen {
      template < typename T >
      decltype(auto) TensorLayoutSwap(T&& t)
      {
        return Eigen::TensorLayoutSwapOp<typename std::remove_reference<T>::type>(t);
      }
    }
    
    int main()
    {
      std::vector<float> v = { 1, 2, 3, 4 };
      auto mapped_t = Eigen::TensorMap<Eigen::Tensor<float, 2, Eigen::RowMajor>>(&v[0], 2, 2);
      Eigen::Tensor<float, 2> t = Eigen::TensorLayoutSwap(mapped_t);
    }