簡體   English   中英

如何將Eigen :: tensor廣播到更高尺寸?

[英]How to broadcast Eigen::Tensor to higher dimensions?

我想將N維Eigen :: Tensor廣播到(N + 1)維Tensor,以做一些簡單的代數。 我找不到正確的語法。

我已經嘗試過就地廣播,並將廣播結果分配給新的張量。 兩者均無法使用大量模板錯誤消息進行編譯(在Mac上使用Apple Clang 10.0.1進行編譯)。 我認為相關的問題是編譯器無法為.resize()找到有效的重載。 我已經嘗試使用std::arrayEigen::array和`Eigen :: Tensor :: Dimensions的廣播操作來獲取尺寸類型,但是沒有一個起作用:

    srand(time(0));
    Eigen::Tensor<float, 3> t3(3, 4, 5);
    Eigen::Tensor<float, 2> t2(3, 4);
    t3.setRandom();
    t2.setRandom();
    // In-place operation
    t3 /= t2.broadcast(std::array<long, 3>{1, 1, 5}); // Does not compile
    // With temporary
    Eigen::Tensor<float, 3> t2b = t2.broadcast(Eigen::array<long, 3>{1, 1, 5}); // Does not compile either
    t3 /= t2b;

這是Eigen :: Tensor不支持的東西嗎?

廣播的工作方式略有不同。 它使用一個參數來指定張量在每個維度上應重復多少次。 這意味着參數數組長度等於張量秩,並且所得張量具有與原始張數相同的秩。

但是,這與您原本的意圖很接近:只需重新塑形!

例如:

    Eigen::Tensor<float, 1> t1(3);
    t1 << 1,2,3;
    auto copies  = std::array<long,1> {3};
    auto shape   = std::array<long,2> {3,3};
    Eigen::Tensor<float,2> t2 = t1.broadcast(copies).reshape(shape);

應該導致包含3 x 3的“矩陣”

1 2 3
1 2 3
1 2 3

暫無
暫無

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

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