How to chop TensorMap?

I understand that the Tensor class supports slicing, but when I tried to chop in a TensorMap instance, the error is that the operation is not supported. How can I slice a TensorMap?

+5
source share
3 answers
std::vector<int> v(27);
std::iota(v.begin(),v.end(),1);

Eigen::TensorMap<Eigen::Tensor<int,3>> mapped(v.data(), 3, 3, 3 );

Eigen::array<long,3> startIdx = {0,0,0};       //Start at top left corner
Eigen::array<long,3> extent = {2,2,2};       // take 2 x 2 x 2 elements 

Eigen::Tensor<int,3> sliced = mapped.slice(startIdx,extent);

std::cout << sliced << std::endl;

This code creates 3 x 3 x 3 TensorMap ( mapped) in a 27-element std-vector ( v), and then cuts a 2 x 2 x 2 ( extent) fragment , starting from the top level, the left corner ( startIdx) and stores it insliced

+1
source

@Kingusiu's answer almost worked for me. This gave a compilation error (VC 2015, Eigen 3.3).

To fix the error, you had to use the auto keyword:

std::vector<int> v(27);
std::iota(v.begin(),v.end(),1);

Eigen::TensorMap<Eigen::Tensor<int,3>> mapped(v.data(), 3, 3, 3 );

Eigen::array<long,3> startIdx = {0,0,0};       //Start at top left corner
Eigen::array<long,3> extent = {2,2,2};       // take 2 x 2 x 2 elements 
auto sliced = mapped.slice(startIdx,extent);
std::cout << sliced << std::endl;

TensorMap 3 x 3 x 3 (mapped) 27 (v), 2 x 2 x 2 (extent), (startIdx), sliced

0

Try

     typedef Eigen::Tensor<float, 2, Eigen::ColMajor, int> TensorType;                                                                                                                                      
      Eigen::TensorMap<TensorType> H(M.data(), 3, 3);
      std::cout << H << std::endl;

Mis 3D matrix, then His 3x3 2D matrix.

-1
source

Source: https://habr.com/ru/post/1667492/


All Articles