我正在尝试从 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/