簡體   English   中英

如何在 Eigen3 中重塑張量?

[英]How to reshape a tensor in Eigen3?

我從 C++ 中的 tensorflow 會話的輸出向量中得到了一些 Eigen::TensorMap。 我想對 Eigen::TensorMap (重塑和連接等)進行一些操作。

但是,由於一些奇怪的錯誤,我的代碼無法編譯。 我試圖用純 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 函數的返回類型是一個 eigen 操作,它將被延遲計算。 TensorRef 是所有張量操作的包裝器。

這段代碼會抱怨:

嚴重性代碼描述項目文件行抑制狀態錯誤 C2679 二進制“=”:未找到采用“const std::array”類型的右側操作數的運算符(或沒有可接受的轉換) testEigen D:\\Programming\\cpp library \\eigen-eigen-323c052e1731\\unsupported\\Eigen\\CXX11\\src\\Tensor\\TensorRef.h 49

你可以混用不同IndexType張量操作(即隱含的第4個模板參數Tensor )。 這也意味着std::array的類型必須與IndexType匹配。 默認情況下, Eigen::Tensor使用與Eigen::Index相同的Eigen::DenseIndex 你可以寫這個而不是Eigen::Tensor<int,4> (類似於Tensor<int,2>

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

或者你用std::array<int, 2> std::array<Eigen::Index, 2>替換std::array<int, 2> std::array<Eigen::Index, 2> 當然,為兩者制作typedef為您提供一些打字的安全,並且可以簡化重構,如果您需要這樣做的話。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM