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?
How to slice a TensorMap?
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);
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
.
That is definitely not a slice! –
Navarrete
© 2022 - 2024 — McMap. All rights reserved.