Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slice a TensorMap?

Tags:

eigen

eigen3

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

like image 331
dalibocai Avatar asked Jan 22 '17 17:01

dalibocai


1 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 a 3 x 3 x 3 TensorMap (mapped) on a std vector of 27 elements (v) and then slices a 2 x 2 x 2 chunk (extent) starting in the top-left-front corner (startIdx) and stores it in sliced

Edit: The type of the result can also be inferred with auto:

auto sliced = mapped.slice(startIdx,extent);
like image 181
kingusiu Avatar answered Jan 04 '23 08:01

kingusiu