Eigen::Tensor,如何从张量访问矩阵 [英] Eigen::Tensor, how to access matrix from Tensor

查看:102
本文介绍了Eigen::Tensor,如何从张量访问矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下特征张量:

Eigen::Tensor<float, 3> m(3,10,10);

我想访问第一个矩阵.在 numpy 我会这样做

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

m(0,:,:)

我将如何在 Eigen 中执行此操作

How would I do this in Eigen

推荐答案

您可以使用 .slice(...).chip(...) 访问张量的一部分.这样做是为了访问第一个矩阵,相当于 numpy m(0,:,:):

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<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} 代替,依此类推.

If you want the "second" matrix, you use offset={1,0,0} instead, and so on.

您可以在此处找到最新文档.

这篇关于Eigen::Tensor,如何从张量访问矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆