gpt4 book ai didi

c++ - 不能用 Eigen 在二维上做收缩?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:00:14 26 4
gpt4 key购买 nike

我正在尝试使用二阶张量 (1 x 1) 在最后两个维度上对三阶张量 (1 x 1 x 1) 进行张量收缩。结果应该是一个 vector 。

下面给出一个断言:

#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;

关于c++ - 不能用 Eigen 在二维上做收缩?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40916832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com