I'm trying to convert the following Python code into its equivalent libtorch:
tfm = np.float32([[A[0, 0], A[1, 0], A[2, 0]],
[A[0, 1], A[1, 1], A[2, 1]]
])
In Pytorch we could simply use torch.stack
or simply use a torch.tensor()
like below:
tfm = torch.tensor([[A_tensor[0,0], A_tensor[1,0],0],
[A_tensor[0,1], A_tensor[1,1],0]
])
However, in libtorch, this doesn't hold, that is I can not simply do:
auto tfm = torch::tensor ({{A.index({0,0}), A.index({1,0}), A.index({2,0})},
{A.index({0,1}), A.index({1,1}), A.index({2,1})}
});
or even using a std::vector
doesn't work. the same thing goes to torch::stack. I'm currently using three torch::stack
to get this done:
auto x = torch::stack({ A.index({0,0}), A.index({1,0}), A.index({2,0}) });
auto y = torch::stack({ A.index({0,1}), A.index({1,1}), A.index({2,1}) });
tfm = torch::stack({ x,y });
So is there any better way for doing this? Can we do this using a one-liner?
所以 C++ libtorch 确实不允许从像 Pytorch 这样的张量列表中构造张量(据我所知),但你仍然可以使用torch::stack
实现这个结果(如果你有兴趣, view
在这里实现)并view
:
auto tfm = torch::stack( {A[0][0], A[1][0], A[2][0], A[0][1], A[1][1], A[2][1]} ).view(2,3);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.