我想将N维特征::张量广播到(N+1(维张量以做一些简单的代数。我无法弄清楚正确的语法。
我已经尝试过就地广播,并将广播结果分配给新的张量。两者都无法编译大量模板错误消息(在 Mac 上使用 Apple Clang 10.0.1 编译(。我认为相关的问题是编译器找不到.resize()
的有效重载。我已经尝试了std::array
、Eigen::array
和'Eigen::Tensor::D imensions对维度类型的广播操作,但没有一个有效:
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