简体   繁体   English

张量流c ++中是否有等效于tf.convert_to_tensor的东西?

[英]Is there an equivalent to tf.convert_to_tensor in tensorflow c++?

I have mean.npy file which I have to convert into tensor. 我有mean.npy文件,我必须将其转换为张量。 I found that tf.convert_to_tensor does that but could not find the equivalent for it in C++. 我发现tf.convert_to_tensor可以做到这一点,但在C ++中找不到与之等效的东西。 Does anybody know the equivalent function in C++ ? 有人知道C ++中的等效功能吗?

There is not a provided way to read a .npy file into a tensorflow::Tensor . 没有提供将.npy文件读入tensorflow::Tensor First you would need to read the file, which is not trivial but it is not too hard either, checkout the NPY format documentation . 首先,您需要阅读该文件,虽然不难,但也不太难,请查看NPY格式文档 Once you have that, the easiest thing would be to copy the data to a tensor: 一旦有了它,最简单的事情就是将数据复制到张量:

// Existing NPY reading function, assuming float type
bool read_npy(const char* file, std::vector<float>& npy_values, std::vector<int64_t>& shape);
// Read file
std::vector<float> npy_values;
std::vector<int64_t> shape;
if (!read_npy("data.npy", npy_values, shape))
{
    // error...
}
// Make tensor
tensorflow::TensorShape tensorShape;
for (int64_t dim : shape)
{
    tensorShape.AddDim(dim);
}
tensorflow::Tensor tensor(DT_FLOAT, tensorShape);
// Copy data
std::copy(npy_values.begin(), npy_values.end(), tensor.flat<float>().data());
// Or with memcpy
std::memcpy(tensor.flat<float>().data(), npy_values.data(), tensor.NumElements() * sizeof(float));

Note that this assumes that the NPY data buffer is in row-major order like TensorFlow tensors, and I suppose IsAligned() should be true for the tensor, although afaik that should always be true for new tensors. 请注意,这假设NPY数据缓冲区像TensorFlow张量那样是行主要顺序,并且我认为IsAligned()对于张量应为true,尽管afaik对于新张量应始终为true。

Another option would be to first create the tensor and then use its buffer ( tensor.flat<float>().data() ) to write the read values. 另一个选择是先创建张量,然后使用其缓冲区( tensor.flat<float>().data() )写入读取值。 This however requires a bit more of work, because you would need to first read the shape of the tensor in the file (or fix it beforehand), create the tensor and then read the file into its buffer (in this case the reading function would receive a pointer and not allocate any memory). 但是,这需要做更多的工作,因为您需要首先读取文件中的张量的形状(或事先对其进行修复),创建张量,然后将文件读取到其缓冲区中(在这种情况下,读取功能会接收指针而不分配任何内存)。

EDIT: I just realised you said "Assuming I have a utility function to read the .npy file and it returns a float pointer to the array", not a vector. 编辑:我刚刚意识到你说“假设我有一个实用程序函数来读取.npy文件,并且它返回指向数组的浮点指针”,而不是向量。 Well the idea should be the same, you can still use memcpy or copy like: 好主意应该是一样的,您仍然可以使用memcpy或类似的copy

std::copy(npy_ptr, npy_ptr + tensor.NumElements(), tensor.flat<float>().data());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM