为什么我不能将运算结果与恒定特征张量数组相乘

时间:2019-07-20 07:25:59

标签: c++ eigen tensor

为什么将张量op(sqrt())与linear.constant()相乘是不合法的

Eigen::Tensor<float, 1> linear(2);
linear.setValues({3,4});
auto linear_square = linear * linear;
auto linear_square_sum = linear_square.sum().sqrt(); 
std::cout<<linear_square_sum<<std::endl; // 5

auto new_linear = linear_square_sum * linear.constant(5); //no compiling error but aborted when executing

错误信息

  

tensor2matrix:   /usr/local/include/unsupported/Eigen/CXX11/src/Tensor/TensorEvaluator.h:380:   Eigen :: TensorEvaluator,Device> :: TensorEvaluator(const XprType&,   const Device&)[with BinaryOp =   Eigen :: internal :: scalar_product_op; LeftArgType =常量   Eigen :: TensorCwiseUnaryOp,   const Eigen :: TensorReductionOp,   const Eigen :: DimensionList,const   Eigen :: TensorCwiseBinaryOp,常量Eigen :: Tensor,常量Eigen :: Tensor

     
    

,Eigen :: MakePointer>>; RightArgType = const Eigen :: TensorCwiseNullaryOp,     const Eigen :: Tensor>;设备= Eigen :: DefaultDevice;     Eigen :: TensorEvaluator,Device> :: XprType =     Eigen :: TensorCwiseBinaryOp,const     Eigen :: TensorCwiseUnaryOp,     const Eigen :: TensorReductionOp,     const Eigen :: DimensionList,const     Eigen :: TensorCwiseBinaryOp,const Eigen :: Tensor,const Eigen :: Tensor     ,Eigen :: MakePointer>>,const Eigen :: TensorCwiseNullaryOp,     const Eigen :: Tensor>>]:断言     `dimensions_match(m_leftImpl.dimensions(),m_rightImpl.dimensions())'     失败了中止

  

1 个答案:

答案 0 :(得分:0)

我敢肯定,人们已经链接到您的文档,说将auto与表达式模板一起使用不是一个好主意。多次评估一个表达模板似乎是个更糟糕的主意。

如果您不想/不能明确提及类型,可以将它们基于您输入的原始值。

Eigen::Tensor<float, 1> linear(2);
linear.setValues({ 3,4 });
decltype(linear) linear_square = linear * linear;
Eigen::Tensor<decltype(linear)::Scalar, 0> linear_square_sum = linear_square.sum().sqrt();
std::cout << linear_square_sum << std::endl; // 5

//              Don't forget to convert to an actual scalar
//                                             |
//                                             v
decltype(linear) new_linear = linear_square_sum() * linear.constant(5);

std::cout << new_linear;

return 0;