void Tensor2EigenMat(torch::Tensor& t, Eigen::MatrixXf& m) {
if (t.dim() == 2) {
m.resize(t.size(1), t.size(0));
//numel() 返回张量中元素的总数,element_size() 返回每个元素所占的字节数
auto num_elements = t.numel();
auto element_size = t.element_size();
auto size_in_bytes = num_elements * element_size;
std::cout << "num_elements=" << num_elements << ", element_size=" << element_size << ", size_in_bytes= " << size_in_bytes << " sizeof(float) * m.size() " << sizeof(float) * m.size();
std::copy(t.data_ptr<float>(), t.data_ptr<float>() + t.numel(), m.data());
}
else {
std::cout << "t.dim()=" << t.dim();
}
}
void Tensor2EigenVec(torch::Tensor& t, Eigen::VectorXf& v) {
if (t.dim() == 1) {
v.resize(t.size(0));
auto num_elements = t.numel();
auto element_size = t.element_size();
auto size_in_bytes = num_elements * element_size;
std::cout << "num_elements=" << num_elements << ", element_size=" << element_size << ", size_in_bytes= " << size_in_bytes << " sizeof(float) * v.size() " << sizeof(float) * v.size();
std::copy(t.data_ptr<float>(), t.data_ptr<float>() + t.numel(), v.data());
}
else {
std::cout << "t.dim()=" << t.dim();
}
}
11-22
1052
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-29
4754
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)