如何让 Eigen::TensorRef 用于广播和其他操作?

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

我正在使用带有 MSVC 的 Eigen 3.4。我以前使用过

Eigen::Ref
,取得了一些成功,我正在尝试使用
Eigen::TensorRef
,但遇到了问题:

Eigen::Tensor<double, 3> A(3, 1, 2);
A.setValues({{{1, 2}},
             {{3, 4}},
             {{5, 6}}});

Eigen::array<int, 3> aBroadcast({1, 4, 1});
auto lazyBroadcastA = A.broadcast(aBroadcast);
const Eigen::TensorRef<const Eigen::Tensor<double, 3>> ref(lazyBroadcastA);

由于最后一行,此代码无法编译。我收到一些错误:

'Dimensions': is not a member of 'Eigen::TensorBroadcastingOp<const std::array<int,3>,const Derived>'   
'IsAligned': is not a member of 'Eigen::TensorBroadcastingOp<const std::array<int,3>,const Derived>'    
'Layout': is not a member of 'Eigen::TensorBroadcastingOp<const std::array<int,3>,const Derived>'   

当我尝试从

Eigen::TensorCwiseUnaryOp
Eigen::TensorReductionOp
构建时,也会发生同样的事情。我是在做一些愚蠢的事情,还是这些操作只是不受支持?

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

惰性广播的结果是

Eigen::TensorBroadcastingOp
,据我了解,它是在
eigen
内实际完成任务的准备实体。大多数操作都有一个
eval
方法来进行计算。同样,我有限的理解是,当使用赋值运算符时,操作会自动评估。

因此,要获得参考,您需要先

eval()
该操作,如下所示:

const Eigen::TensorRef<const Eigen::Tensor<double, 3>> ref(lazyBroadcastA.eval());

我获得该张量的另一种方法是:

Eigen::Tensor<double, 3> A2 = lazyBroadcastA;

当我打印结果时,我得到:

1 1 1 1 2 2 2 2 
3 3 3 3 4 4 4 4 
5 5 5 5 6 6 6 6
© www.soinside.com 2019 - 2024. All rights reserved.