テンソルのリストをトーチ::テンソルに変換する方法は?
Aug 23 2020
次のPythonコードを同等のlibtorchに変換しようとしています。
tfm = np.float32([[A[0, 0], A[1, 0], A[2, 0]],
[A[0, 1], A[1, 1], A[2, 1]]
])
Pytorchtorch.stack
では、torch.tensor()
以下のようなものを使用することも、単に使用することもできます。
tfm = torch.tensor([[A_tensor[0,0], A_tensor[1,0],0],
[A_tensor[0,1], A_tensor[1,1],0]
])
ただし、libtorchでは、これは当てはまりません。つまり、単純に行うことはできません。
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})}
});
またはを使用しstd::vector
ても機能しません。同じことがtorch :: stackにも当てはまります。私は現在torch::stack
、これを行うために3つを使用しています。
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 });
それで、これを行うためのより良い方法はありますか?ワンライナーを使用してこれを行うことはできますか?
回答
1 trialNerror Aug 24 2020 at 18:46
したがって、C ++ libtorchは、Pytorchのようなテンソルのリストからのテンソル構築を実際には許可していません(私が知る限り)が、torch::stack
(興味がある場合はここで実装されます)と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);