简体   繁体   中英

most efficient way to create tensorflow::tensor from std::vector

So my question is to know if there is a way to pass directly the values from a vector (but we could also think about array ) to a tensorflow::tensor ?

The only way I know is to copy each value one by one.

Example (2D Vector) :

tensorflow::Tensor input(tensorflow::DT_FLOAT, tensorflow::TensorShape({50, 20})); 
auto input_map = input.tensor<float, 2>();


for (int b = 0; b < 50; b++) {
  for (int c = 0; c < 20; c++) {
    input_map(b, c) = (vector_name)[b][c];
  }
}

Is there more convenient ways to do it?

For example array to vector :

int x[3] = {1, 2, 3};
std::vector<int> v(x, x + sizeof x / sizeof x[0]);

how about this? std::copy_n(vec.begin(), vec.size(), input.flat<float>().data())

Have you tried tf.convert_to_tensor ? Maybe something like tf.convert_to_tensor(value, as_ref=True)

https://www.tensorflow.org/versions/r0.11/api_docs/python/framework.html#convert_to_tensor

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