'How can I change 3D Libtorch tensor into 3D eigen nested array(or matrix)
I need the 3D eigen nested array, which can be transfered from Libtorch tensor. The example code is as follows:
typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> MatrixXf_rm;
typedef Eigen::Array<MatrixXf_rm, Eigen::Dynamic, 1> DMVector;
int main()
{
std::cout << "Testing Libtorch to Eigen" << std::endl;
auto T = torch::rand({3, 3, 2});
std::cout << "Libtorch:" << std::endl;
std::cout << T << std::endl;
DMVector result = DMVector::Constant(3, 1, MatrixXf_rm());
for (int i = 0; i < 3; i++)
{
float* data = T[i].data_ptr<float>();
Eigen::Map<MatrixXf_rm> E(data, T[i].size(0), T[i].size(1));
result(i) = E;
}
return 0;
}
I can get the right result from the above code. But I don not want to do it in a for loop way: for (int i = 0; i < 3; i++). Is there a solution that can get the same result as the above code without the for loop?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
