Eigen::Tensor,如何从张量访问矩阵
Eigen::Tensor, how to access matrix from Tensor
我有以下本征张量:
Eigen::Tensor<float, 3> m(3,10,10);
我想访问第一个矩阵。在 numpy 中我会这样做
m(0,:,:)
我如何在 Eigen 中做到这一点
您可以使用 .slice(...)
或 .chip(...)
访问张量的一部分。这样做是为了访问第一个矩阵,相当于 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.
如果您想要“第二个”矩阵,请改用 offset={1,0,0}
,依此类推。
我有以下本征张量:
Eigen::Tensor<float, 3> m(3,10,10);
我想访问第一个矩阵。在 numpy 中我会这样做
m(0,:,:)
我如何在 Eigen 中做到这一点
您可以使用 .slice(...)
或 .chip(...)
访问张量的一部分。这样做是为了访问第一个矩阵,相当于 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.
如果您想要“第二个”矩阵,请改用 offset={1,0,0}
,依此类推。