Reputation: 139
I would like to broadcast an N-dimensional Eigen::Tensor to an (N+1)-dimensional Tensor to do some simple algebra. I cannot figure out the correct syntax.
I have already tried broadcasting in-place, and assigning the result of the broadcast to a new tensor. Both fail to compile with substantial template error messages (compiling on Mac with Apple Clang 10.0.1). I think the relevant problem is that the compiler cannot find a valid overload for .resize()
. I have tried the broadcasting operation with both std::array
, Eigen::array
, and `Eigen::Tensor::Dimensions for the type of the dimensions but none worked:
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;
Is this something Eigen::Tensor does not support?
Upvotes: 4
Views: 1519
Reputation: 516
Broadcast works in a slightly different way. It takes an argument specifying how many times the tensor should be repeated in each dimension. This means both that the argument array length is equal to the tensor rank, and that the resulting tensor has the same rank as the original.
However, this is close to what you had intended originally: just add a reshape!
For instance:
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);
Should result in a 3 by 3 "matrix" containing
1 2 3
1 2 3
1 2 3
Upvotes: 1