简体   繁体   English

Eigen::Tensor,如何从张量访问矩阵

[英]Eigen::Tensor, how to access matrix from Tensor

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在 numpy 我会这样做

m(0,:,:)

How would I do this in Eigen我将如何在 Eigen 中做到这一点

You can access parts of a tensor using .slice(...) or .chip(...) .您可以使用.slice(...).chip(...)访问张量的.slice(...) Do this to access the first matrix, equivalent to numpy m(0,:,:) :这样做可以访问第一个矩阵,相当于 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.如果您想要“第二个”矩阵,则使用offset={1,0,0}代替,依此类推。

You can find the most recent documentation here .您可以 在此处找到 最新的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM