LibTorch C++ 和 Eigen 之间的数据传输(问题和帮助)
大家好,我正在为 C++ 线性代数库开发数据传输工具,您可以在此处看到: https ://github.com/andrewssobral/dtt (考虑二维数组或矩阵),我想知道您是否可以帮助我完成以下代码在 LibTorch 和 Eigen 之间进行数据传输:
std::cout << "Testing LibTorch to Eigen:" << std::endl;
// LibTorch
torch::Device device(torch::cuda::is_available() ? torch::kCUDA : torch::kCPU);
torch::Tensor T = torch::rand({3, 3});
std::cout << "LibTorch:" << std::endl;
std::cout << T << std::endl;
// Eigen
float* data = T.data_ptr<float>();
Eigen::Map<Eigen::MatrixXf> E(data, T.size(0), T.size(1));
std::cout << "EigenMat:\n" << E << std::endl;
// re-check after changes
E(0,0) = 0;
std::cout << "EigenMat:\n" << E << std::endl;
std::cout << "LibTorch:" << std::endl;
std::cout << T << std::endl;
这是代码的输出:
--------------------------------------------------
Testing LibTorch to Eigen:
LibTorch:
0.6232 0.5574 0.6925
0.7996 0.9860 0.1471
0.4431 0.5914 0.8361
[ Variable[CPUFloatType]{3,3} ]
EigenMat (after data transfer):
0.6232 0.7996 0.4431
0.5574 0.986 0.5914
0.6925 0.1471 0.8361
# Modifying EigenMat, set element at (0,0) = 0
EigenMat:
0 0.7996 0.4431
0.5574 0.986 0.5914
0.6925 0.1471 0.8361
# Now, the LibTorch matrix was also modified (OK), but the rows and columns were switched.
LibTorch:
0.0000 0.5574 0.6925
0.7996 0.9860 0.1471
0.4431 0.5914 0.8361
[ Variable[CPUFloatType]{3,3} ]
有人知道发生了什么吗?有更好的方法吗?
我还需要对 Armadillo、ArrayFire 和 OpenCV (cv::Mat) 做同样的事情。提前致谢!