我应该如何获得从征3张量切片的Vector?

问题描述 投票:0回答:1

我撕我的头发试图在Eigen::Tensor<double, 3>作为Eigen::VectorXd访问数据的列。

切片,as according to this answer,做工精细,让我我想要的列。但我不能然后分配到一个载体。

我有的:

Eigen::Tensor<double, 3> my_tens(2, 3, 4);
my_tens.setRandom();

Eigen::array<Eigen::Index, 3> dims = my_tens.dimensions();

Eigen::array<Eigen::Index, 3> offsets = {0, 1, 0};
Eigen::array<Eigen::Index, 3> extents = {dims[0], 1, 1};

// This works perfectly, and is exactly the column I want:
std::cout << my_tens.slice(offsets, extents);

// ... and I want it as a VectorXd, so:
Eigen::VectorXd my_vec(dims[0]);

下面的事情我已经尝试了所有失败:

// Direct assignment (won't compile, no viable overloaded '=')
my_vec = my_tens.slice(offsets, extents);

// Initialisation (won't compile, no viable overloaded '<<')
my_vec << my_tens.slice(offsets, extents);

// Same problem with reshaping:
g_a = signature_a.g.slice(offsets, extents).reshape(Eigen::array<Eigen::Index, 2>{dims[0], 1});

// Converting the base (won't compile, no member 'matrix')
my_vec << my_tens.slice(offsets, extents).matrix();

我也试过映射as in this answer,但是,这并不工作,要么(编辑:我认为这是由于到存储排序,但实际上是由于不正确的偏移量,请看我的回答):

// This produces a part of a row of the tensor, not a column. Gah!
auto page_offset = offsets[2] * dims[0] * dims[1];
auto col_offset = offsets[1] * dims[0];
auto bytes_offset = sizeof(double) * (page_offset + col_offset)
Eigen::Map<Eigen::VectorXd> my_mapped_vec(my_tens.data() + bytes_offset, dims[0]);

如果真的这么难,还是我失去了一些东西简单?感谢您的任何和所有帮助!

c++ c++11 eigen tensor eigen3
1个回答
0
投票

回答我的问题:是的,我失去了一些东西简单。通过对比数字我下了地图操作的我意识到的偏移量为8出的一个因素;即通过出sizeof(double)

我没有意识到该操作my_tens.data() + bytes_offset需要my_tens.data(),一个const double *,和而不是添加字节固定数量的,以抵消指针,由该数量的元件的偏移它。

下面是正确的代码:

Eigen::Tensor<double, 3> my_tens(2, 3, 4);
my_tens.setRandom();
Eigen::array<Eigen::Index, 3> dims = my_tens.dimensions();
Eigen::array<Eigen::Index, 3> offsets = {0, 1, 0};
Eigen::array<Eigen::Index, 3> extents = {dims[0], 1, 1};

// Compute the offset, correctly this time!
auto page_offset = offsets[2] * dims[0] * dims[1];
auto col_offset = offsets[1] * dims[0];
auto elements_offset = page_offset + col_offset;

// Map into the array
Eigen::Map<Eigen::VectorXd> my_mapped_vec(my_tens.data() + elements_offset, dims[0]);

// Compare the two:
std::cout << my_tens.slice(offsets, extents) << std::endl;
std::cout << my_mapped_vec.transpose() << std::endl;
© www.soinside.com 2019 - 2024. All rights reserved.