【问题标题】:Eigen: strange behaviour of tensor multiplication operators特征:张量乘法运算符的奇怪行为
【发布时间】:2017-04-10 08:15:13
【问题描述】:

我想使用 Eigen 的张量类。 我已经安装了 Eigen 3.3.3 并确保我的项目选择了正确的版本(而不是旧的 ubuntu 包)

cout<<EIGEN_WORLD_VERSION<<"."<<EIGEN_MAJOR_VERSION<<"."<<EIGEN_MINOR_VERSION<<endl;

// prints 3.3.3

此代码有效:

Tensor<double, 3> t(3,3,3);
t.setConstant(1);

但是这段代码失败了:

t*=5;

错误信息抱怨int类型:

[ 50%] Building CXX object CMakeFiles/sandbox.dir/src/SandBox/sandbox.cpp.o
In file included from /opt/eigen333/include/eigen3/unsupported/Eigen/CXX11/Tensor:103:0,
                 from /home/lars/programming/fsd_cpp/src/SandBox/sandbox.cpp:3:
/opt/eigen333/include/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h: In instantiation of ‘Derived& Eigen::TensorBase<Derived, AccessLevel>::operator*=(const OtherDerived&) [with OtherDerived = int; Derived = Eigen::Tensor<double, 3>; int AccessLevel = 1]’:
/home/lars/programming/fsd_cpp/src/SandBox/sandbox.cpp:30:6:   required from here
/opt/eigen333/include/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h:876:36: error: request for member ‘derived’ in ‘other’, which is of non-class type ‘const int’
       return derived() = derived() * other.derived();

我尝试过t*=5.;,但这只会将错误消息更改为which is of non-class type ‘const double’

当我将代码更改为:

t=t*5;

错误信息变得相当长:https://pastebin.com/T6kvLaZ9

最终版本:

t=t*5.;

令人惊讶的是,这行得通。我不明白为什么t*=5.; 会产生错误。

【问题讨论】:

    标签: c++ templates eigen eigen3 tensor


    【解决方案1】:

    如前所述,使用 * 运算符的标量乘法目前不受 Eigen 张量支持。然而,逐元素张量乘法是。要对张量进行标量乘法,请使用命令t.constant(Scalar) 创建一个与t 尺寸相同但所有元素都等于Scalar 的值的张量,前提是Scalarfloat 类型。然后使用* 运算符乘以这个张量:

    Tensor<double, 3> t(5,5);
    t.setConstant(1);
    
    Tensor<double, 3> ans(5,5) = t*t.constant(5.);
    std::cout << t << std::endl;
    std::endl;
    std::cout << ans << std::endl;
    

    生产

    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    
    5 5 5 5 5
    5 5 5 5 5
    5 5 5 5 5
    5 5 5 5 5
    5 5 5 5 5
    

    【讨论】:

      【解决方案2】:

      据我所知,当前的 Eigen Tensor 实现不支持标量乘法。 但是你可以尝试将 Tensor 转换为 Matrix。

      【讨论】:

      • 很难说什么支持什么不支持,文档正在进行中。但我发现了这个:eigen.tuxfamily.org/…。有一个“支持的功能”列表,其中包含项系数操作(加法、减法、...)
      • 张量到矩阵的转换是不可能的,我有超过 2 个维度
      猜你喜欢
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      相关资源
      最近更新 更多