[英]How is it possible to convert a std::vector<std::vector<double>> to a torch::Tensor?
I have a std::vector<std::vector<double>>
where I want to conver it into a torch::Tensor
in libtorch.我有一个std::vector<std::vector<double>>
,我想将它转换成 libtorch 中的torch::Tensor
。 However it seems, the torch::tensor()
, or torch::from_blob()
, can't be used for this purpose!然而, torch::tensor()
或torch::from_blob()
似乎不能用于此目的!
I tried to use c10::ArrayRef
and then use that for converting the data into a torch::Tensor
by doing c10::ArrayRef<std::vector<std::vector<double>>> res(myvecs)
but this also seems useless as I can't seem to find a way to convert it into torch::Tensor
.我尝试使用c10::ArrayRef
,然后通过执行c10::ArrayRef<std::vector<std::vector<double>>> res(myvecs)
将数据转换为torch::Tensor
,但这也是似乎没用,因为我似乎找不到将其转换为torch::Tensor
的方法。
How should I go about this conversion in libtorch?我应该如何 go 关于 libtorch 中的这种转换? What are my other options other than eg:除了例如:我还有什么其他选择:
auto tensor = torch::zeros({ 46,85 });
for (size_t i = 0; i < 46; i++)
{
for (size_t j = 0; j < 85; j++)
{
tensor[i][j] = probs[i][j];
}
}
the easiest way would be to use a simple std::vector<double>
instead of a vector of vectors.最简单的方法是使用简单的std::vector<double>
而不是向量的向量。 You would have contiguous memory and torch::from_blob
would work (as mentionned in the other answer).您将拥有连续的 memory 并且torch::from_blob
可以工作(如其他答案中所述)。
If that is not possible/convenient, I suggest the following workaround.如果那不可能/不方便,我建议采用以下解决方法。 I assume that your vector is a (n,m)
matrix (ie all the n
vectors have the same size m
):我假设你的向量是一个(n,m)
矩阵(即所有n
向量都具有相同的大小m
):
int n = 5, m = 4;
// Just creating some dummy data for example
std::vector<std::vector<double>> vect(n, std::vector<double>(m, 0));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
vect[i][j] = i+j;
// Copying into a tensor
auto options = torch::TensorOptions().dtype(at::kDouble);
auto tensor = torch::zeros({n,m}, options);
for (int i = 0; i < n; i++)
tensor.slice(0, i,i+1) = torch::from_blob(vect[i].data(), {m}, options);
Edit: you may need to add a call to clone
in case where you cannot ensure that the vector will outlive the tensor (because from_blob
do not take ownership, so its data will be erased when the vector is destroyed)编辑:您可能需要添加对clone
的调用,以防您无法确保向量的寿命超过张量(因为from_blob
不取得所有权,因此当向量被销毁时其数据将被删除)
I have not used any of the libraries that you mention, but if I should guess then the libraries probably expect a continuous array and not small segments of memory scattered around the heap.我没有使用过你提到的任何库,但如果我猜的话,这些库可能需要一个连续的数组,而不是散布在堆周围的 memory 的小段。
So convert the std::vector<std::vector<double>>
to std::vector<double>
and pass the vec.data()
pointer to torch
所以将std::vector<std::vector<double>>
转换为std::vector<double>
并将vec.data()
指针传递给torch
std::vector<double> linearize(const std::vector<std::vector<double>>& vec_vec) {
std::vector<double> vec;
for (const auto& v : vec_vec) {
for (auto d : v) {
vec.push_back(d);
}
}
return vec;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.