Eigen::Tensor, how to access matrix from Tensor

对着背影说爱祢 提交于 2019-12-03 13:56:43

问题


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


回答1:


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 
Eigen::array<long,3> offset = {0,0,0};         //Starting point
Eigen::array<long,3> extent = {1,10,10};       //Finish point 
std::cout <<  m.slice(offset, extent).reshape(Eigen::array<long,2>{10,10}) << std::endl;  //Reshape the slice 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.



来源:https://stackoverflow.com/questions/48650751/eigentensor-how-to-access-matrix-from-tensor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!