c++ - 如何在 Eigen3 中 reshape 张量?

标签 c++ tensor eigen3

我从 C++ 中的 tensorflow session 的输出 vector 中获得了一些 Eigen::TensorMap。我想对 Eigen::TensorMap 进行一些操作( reshape 和连接等)。

但是,由于一些奇怪的错误,我的代码无法编译。 我尝试用纯 Eigen3 代码重现它。

#include <unsupported/Eigen/CXX11/Tensor>
using Eigen::Tensor;
using Eigen::TensorMap;
using Eigen::TensorRef;
using std::vector;
int main() {
    int storage[128];
    TensorMap<Tensor<int, 4>> t_4d(storage, 2, 4, 2, 8);
    vector<TensorRef<Tensor<int,2>>> reshapedTensors;
    std::array<int, 2> shape{ 16,8 };
    auto re_op = t_4d.reshape(shape);
    reshapedTensors.push_back(re_op);
    return 0;
}

根据Eigen Doc ,reshape函数的返回类型是特征运算,它将被延迟计算。 TensorRef 是所有张量操作的包装器。

这段代码会提示:

严重性代码描述项目文件行抑制状态 错误 C2679 二进制“=”:找不到采用“const std::array”类型的右侧操作数的运算符(或者没有可接受的转换)testEigen D:\Programming\cpp library\eigen-eigen-323c052e1731\unsupported\Eigen\CXX11\src\Tensor\TensorRef.h 49

最佳答案

你不能混合不同的IndexType用于张量运算(即 Tensor 的隐式第四个模板参数)。这也意味着 std::array 的类型必须匹配IndexType 。默认情况下,Eigen::Tensor使用Eigen::DenseIndexEigen::Index 相同。您可以写这个而不是 Eigen::Tensor<int,4> (类似于 Tensor<int,2> )

Eigen::Tensor<int, 4, 0, int>

或者你替换 std::array<int, 2>通过std::array<Eigen::Index, 2> 。 当然制作typedef两者的 s 可以保护您的输入安全,并且可以简化重构(如果您需要这样做的话)。

关于c++ - 如何在 Eigen3 中 reshape 张量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56985731/

相关文章:

c++ - 使用 C++ 的汇编语言

python - theano中的张量到底是什么?

c++ - 基于索引 vector 的特征矩阵切片

c++ - Eigen3 JacobiSVD 不同的奇异值取决于编译器标志

python - 可转换为 TensorRT 的 Tensorflow tf.abs() 替代方案

c++ - 带有 fadbad 的特征向量

c++ - WIll Boost的版本带有现代C++ "cutoff"吗?

c++ - 最准确,但是 "cheapest"测量 C++ 代码的方法?

c++ - 如何在发布中排除测试用例(gTest)

python - pytorch张量根据列对行进行排序