由于某种原因,在编译后,以下段对我来说是错误的:
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
Similarly, the inner product of 2 1d tensors (through contractions) returns a 1d tensor.
编辑:以下给出断言错误:
#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);
关于c++ - 为什么会出现以下段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40614417/