简体   繁体   中英

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

I have mean.npy file which I have to convert into tensor. I found that tf.convert_to_tensor does that but could not find the equivalent for it in C++. Does anybody know the equivalent function in C++ ?

There is not a provided way to read a .npy file into a 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 . 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.

Another option would be to first create the tensor and then use its buffer ( tensor.flat<float>().data() ) to write the read values. 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. Well the idea should be the same, you can still use memcpy or copy like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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