How to slice a TensorMap?
Asked Answered
D

2

6

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?

Deal answered 22/1, 2017 at 17:50 Comment(0)
A
4
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);
Andesite answered 28/3, 2018 at 14:41 Comment(0)
D
-2

Try

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

M is a 3D matrix, whereas H is a 3x3 2D matrix.

Dupree answered 8/5, 2017 at 15:42 Comment(1)
That is definitely not a slice!Navarrete

© 2022 - 2024 — McMap. All rights reserved.