Hey,
I also needed to transfer data from Eigen to libtorch, so I inspected your example. For a square matrix, it seems that it is a transpose porblem, but if you try the same thing with a non-square matrix, the result is chaotic
After a bit of thinking, I came out with this solution (I tested on square and non square matrix).
torch::Tensor eigenVectorToTorchTensor(VectorXd e){
auto t = torch::rand({e.rows()});
float* data = t.data_ptr<float>();
Map<VectorXf> ef(data,t.size(0),1);
ef = e.cast<float>();
t.requires_grad_(true);
return t;
}
torch::Tensor eigenMatrixToTorchTensor(MatrixXd e){
auto t = torch::rand({e.cols(),e.rows()});
float* data = t.data_ptr<float>();
Map<MatrixXf> ef(data,t.size(1),t.size(0));
ef = e.cast<float>();
t.requires_grad_(true);
return t.transpose(0,1);
}
NB: Here I am interested into converting an Eigen object to a torch object, but I think that you can easily get the reverse function from this code.
Hope it helps!
A+
/jeremy