为什么会出现以下段故障

Why does the following segfault?

本文关键字:故障 段故障 为什么      更新时间:2023-10-16

在使用

编译后,由于某种原因,下面的segg出现了错误:
g++ 1.cpp -I/path_to_eigen/eigen -std=c++0x

它应该在两个相同长度的秩为1的张量之间做点积(因此得到秩为1维数为1的张量)

#include <Eigen/Core>
#include <unsupported/Eigen/CXX11/Tensor>
#include <iostream>
#include <array>
using namespace Eigen;
using namespace std;

int main()
{
        Eigen::Tensor<double, 1> tensor(5);
        Eigen::Tensor<double, 1> tensor2(5);
        std::array<Eigen::IndexPair<int>, 1> product_dims = { IndexPair<int>(0, 0) };
        Eigen::Tensor<double, 1> tensor3(1);
        tensor3 = tensor.contract(tensor2, product_dims);
}
注意:如果我改变
        tensor3 = tensor.contract(tensor2, product_dims);

        auto v = tensor.contract(tensor2, product_dims);

然后它编译,并执行没有分段,但我不确定类型v是什么!我需要它是一个秩为1、维数为1的张量,如文档中所述:

https://github.com/RLovelett/eigen/blob/master/unsupported/Eigen/CXX11/src/Tensor/README.md

同样,2维张量的内积(通过收缩)返回一个1d张量。

编辑:下面给出一个断言错误:

#include <Eigen/Core>
#include <unsupported/Eigen/CXX11/Tensor>
#include <iostream>
#include <array>
using namespace Eigen;
using namespace std;

int main()
{
        Eigen::Tensor<double, 1> tensor(5);
        Eigen::Tensor<double, 1> tensor2(5);
        tensor.setConstant(1);
        tensor2.setConstant(2);
        tensor(1) = 1;
        tensor2(1) = 2;
        std::array<Eigen::IndexPair<int>, 1> product_dims = { IndexPair<int>(0, 0) };
        Eigen::Tensor<double, 1> tensor3(1);
        tensor3.setConstant(0);
        auto v = tensor.contract(tensor2, product_dims);
        cerr<<v<<endl;
        tensor3 = tensor3 + v;
        cerr<<tensor3<<endl;
}

我现在用tensor3 = tensor3 + v代替直接赋值v tensor3

错误是:

Assertion failed: (dimensions_match(m_leftImpl.dimensions(), m_rightImpl.dimensions())), function TensorEvaluator, file /Users/eigen/unsupported/Eigen/CXX11/src/Tensor/TensorEvaluator.h, line 355.

文档已经过时了。压缩结果的秩由以下公式给出:RankL + RankR - 2*CDims,其中RankL为第一个输入张量的秩,RankR为第二个输入张量的秩,CDims为压缩维数。

在您的示例中,结果的秩为0。你应该写Eigen::Tensor<double, 0> tensor3 = tensor.contract(tensor2, product_dims);