问题描述
我正在尝试将以下Python代码转换为等效的libtorch:
tfm = np.float32([[A[0,0],A[1,A[2,0]],[A[0,1],1]]
])
在Pytorch中,我们可以像下面这样简单地使用torch.stack
或简单地使用torch.tensor()
:
tfm = torch.tensor([[A_tensor[0,A_tensor[1,[A_tensor[0,0]
])
但是,在libtorch中,这不成立,也就是说我不能简单地这样做:
auto tfm = torch::tensor ({{A.index({0,0}),A.index({1,A.index({2,0})},{A.index({0,1}),1})}
});
甚至无法使用std::vector
都不起作用。同样的事情也可以用于torch :: stack。我目前正在使用三个torch::stack
来完成此操作:
auto x = torch::stack({ A.index({0,0}) });
auto y = torch::stack({ A.index({0,1}) });
tfm = torch::stack({ x,y });
那么还有什么更好的方法吗?我们可以使用单线吗?
解决方法
所以C ++ libtorch确实不允许从像Pytorch这样的张量列表中进行张量构造(据我所知),但是您仍然可以使用torch::stack
(如果实现here您有兴趣)和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);