【发布时间】:2022-01-28 07:36:36
【问题描述】:
为Pytorch 使用 C++ libtorch 前端
我想从 C++ double[] 数组创建一个 torch::Tensor。来自旧版 C/C++ API。
我在docs 和论坛中都找不到有关该主题的简单文档。
类似:
double array[5] = {1, 2, 3, 4, 5}; // or double *array;
auto tharray = torch::Tensor(array, 5, torch::Device(torch::kCUDA));
我发现的唯一方法是使用torch::from_blob,但如果我想将它与CUDA 一起使用,我将不得不使用clone() 并使用to(device)。
double array[] = { 1, 2, 3, 4, 5};. // or double *array;
auto options = torch::TensorOptions().dtype(torch::kFloat64);
torch::Tensor tharray = torch::from_blob(array, {5}, options);
有没有更清洁的方法?
【问题讨论】:
-
您可以在创建张量的同时使用
TensorOptions设置设备吗?类似auto options = torch::TensorOptions().dtype(torch::kFloat64).device(torch::kCUDA, 1)