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]);
A std::vector can never be faster than an array, as it has (a pointer to the first element of) an array as one of its data members. But the difference in run-time speed is slim and absent in any non-trivial program. One reason for this myth to persist, are examples that compare raw arrays with mis-used std::vectors.
If you need a "dynamic" array, then std::vector is the natural solution. It should in general be the default container for everything. But if you want a statically sized array created at time of compilation (like a C-style array is) but wrapped in a nice C++ object then std::array might be a better choice.
There are four main tensor type you can create: tf. Variable. tf.
All tensors are immutable like Python numbers and strings: you can never update the contents of a tensor, only create a new one.
how about this?
std::copy_n(vec.begin(), vec.size(), input.flat<float>().data())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With