Eigen::Tensor, how to access matrix from Tensor

前端 未结 1 1879
温柔的废话
温柔的废话 2021-02-10 00:51

I have the following Eigen Tensor:

Eigen::Tensor m(3,10,10);

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

1条回答
  •  无人共我
    2021-02-10 01:14

    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 m(3,10,10);          //Initialize
    m.setRandom();                               //Set random values 
    std::array offset = {0,0,0};         //Starting point
    std::array extent = {1,10,10};       //Finish point
    std::array 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.

    0 讨论(0)
提交回复
热议问题