c++ - 如何使用指针用数据填充 xtensor 数组

标签 c++ caffe xtensor

我正在尝试从 caffe 库中的 blob 数据创建一个 xtensor 数组。使用 caffe 中的函数 mutable_cpu_data() 返回指向数据的指针,例如 float* data = output->mutable_cpu_data();。这可以用 xtensor 实现吗?如果是,能否请您举个例子。我找到了使用 OpenCV Mat 的例子,但是 xtensor 很像 numpy 这使得像数据这样的矩阵操作变得更加容易。

最佳答案

您可以使用 xadapt.hpp 中的 xt::adapt 函数,但您需要提供一个形状:

float* data = output->mutable_cpu_data();
size_t size = size_of_data;
// For a 1D tensor for instance
xt::static_shape<std::size_t, 1> sh = { size_of_data};
// Parameters of adapt are:
// - the 1D buffer to adapt
// - the size of the buffer
// - the ownership flag (should the adaptor destroy your buffer upon deletion, here
//   probably not)
// - the shape
auto a = xt::adapt(data, size_of_data, false sh);

与 Naidu 提供的解决方案相比,优势在于您无需复制数据缓冲区,它是“就地”适配的。

关于c++ - 如何使用指针用数据填充 xtensor 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52740327/

相关文章:

c++ - 使用管道加密文件

c++ - 在 C++ 中模板化一个 'for' 循环?

machine-learning - HDF5中文件数量小于batch时会发生什么

c++ - 在 CMake CMakeLists.txt 中添加对 NewT 的库引用

c++ - 如何在 C++ 中编写 FCC、BCC 和 HCP 晶格数组

c++ - NVCC 和 Boost 1.60 错误 : identifier "__float128" is undefined

machine-learning - 修改GoogLeNet中的Deploy.prototxt

Numpy vs Eigen vs Xtensor 线性代数基准奇数

python - xtensor 的 "operator/"比 numpy 的 "/"慢