如何将PyTrac张量转换成C++ Trof:张量反之亦然?

2024-04-24 23:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在PyBund11中使用C++模块中包含Pyrun张量,并返回结果字典,其中包括C++ + Trop::张量返回。就我所寻找的,似乎没有明确的方法将PyTorch Tensor转换成C++张量,C++的张量到PyTrac张量。在最后一次尝试中,我尝试将PyObject转换为torch::Tensor,但似乎效果不佳。(https://discuss.pytorch.org/t/is-it-possible-to-get-pyobject-from-a-torch-tensor/85980/2)我想知道它是否正确,是否有任何解决办法。我在下面分享我的代码片段

py::dict quantize(py::dict target) {
    ...
    for (auto item: target) {
        py::str key(item.first);
        torch::Tensor test = item.second.ptr(); // it fails to compile
    }
    ...
    return py::dict("name"_a="test", "tensor"_a=torch::rand({3, 3, 3})); // it fails on runtime
}

Tags: 模块topytesttarget字典ittorch