不能用本征在二维上收缩吗?

can't do contraction on two dimensions with eigen?

本文关键字:二维 不能      更新时间:2023-10-16

我试图在最后两个维度上进行第三级张量(1 x 1 x 1)的张量收缩,并使用两个等级张量(1 x 1)进行张量。结果应该是向量。

以下主张:

#include <Eigen/Core>
#include <unsupported/Eigen/CXX11/Tensor>
#include <iostream>
#include <array>
#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
#include <mutex>

using namespace Eigen;
using namespace std;

int main()
{
        Eigen::Tensor<double, 3> tensor(1, 1, 1);
        Eigen::Tensor<double, 2> tensor2(1,1);
        Eigen::Tensor<double, 1> tensor1;
        std::array<Eigen::IndexPair<int>, 1> product_dims;
        product_dims[0] =  { IndexPair<int>(1, 0) };
        product_dims[1] =  { IndexPair<int>(2, 1) };
        auto vv = tensor.contract(tensor2, product_dims);
        cerr<<"value: "<<vv<<endl;
        tensor1 = vv;
}

但打印值:0如预期。

断言是:

a.out: /eigen-eigen-7c567a7c10e1/unsupported/Eigen/CXX11/src/Tensor/TensorAssign.h:125: bool Eigen::TensorEvaluator<const Eigen::TensorAssignOp<LhsXprType, RhsXprType>, Device>::evalSubExprsIfNeeded(Eigen::TensorEvaluator<const Eigen::TensorAssignOp<LhsXprType, RhsXprType>, Device>::Scalar*) [with LeftArgType = Eigen::Tensor<double, 0>; RightArgType = const Eigen::TensorContractionOp<const std::array<Eigen::IndexPair<int>, 1ul>, const Eigen::Tensor<double, 3>, const Eigen::Tensor<double, 2> >; Device = Eigen::DefaultDevice; Eigen::TensorEvaluator<const Eigen::TensorAssignOp<LhsXprType, RhsXprType>, Device>::Scalar = double]: Assertion `dimensions_match(m_leftImpl.dimensions(), m_rightImpl.dimensions())' failed.

有什么想法为什么我有这个维度不匹配的主张?

您的数组是一个大小1,因此您有效地收缩了一对尺寸。相反,您应该创建数组如下:

std::array<Eigen::IndexPair<int>, 2> product_dims;