[英]Eigen::Tensor, how to access matrix from Tensor
我有以下特征張量:
Eigen::Tensor<float, 3> m(3,10,10);
我想訪問第一個矩陣。 在 numpy 我會這樣做
m(0,:,:)
我將如何在 Eigen 中做到這一點
您可以使用.slice(...)
或.chip(...)
訪問張量的.slice(...)
。 這樣做可以訪問第一個矩陣,相當於 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}
代替,依此類推。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.