Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen::Tensor, how to access matrix from Tensor

Tags:

c++

tensor

eigen3

I have the following Eigen Tensor:

Eigen::Tensor<float, 3> m(3,10,10);

I want to access the 1st matrix. In numpy I would do it as such

m(0,:,:)

How would I do this in Eigen

like image 531
raaj Avatar asked Feb 06 '18 19:02

raaj


1 Answers

You can access parts of a tensor using .slice(...) or .chip(...). Do this to access the first matrix, equivalent to numpy m(0,:,:):

Eigen::Tensor<double,3> m(3,10,10);          //Initialize
m.setRandom();                               //Set random values 
std::array<long,3> offset = {0,0,0};         //Starting point
std::array<long,3> extent = {1,10,10};       //Finish point
std::array<long,2> shape2 = {10,10};         //Shape of desired rank-2 tensor (matrix)
std::cout <<  m.slice(offset, extent).reshape(shape2) << std::endl;  //Extract slice and reshape it into a 10x10 matrix.

If you want the "second" matrix, you use offset={1,0,0} instead, and so on.

You can find the most recent documentation here.

like image 98
DavidAce Avatar answered Sep 29 '22 14:09

DavidAce