简体   繁体   中英

Pass Vector of Floats to Tensor in Tensorflow C++

I have an LSTM frozen graph that takes 10 sequences, and each input sequence has 10 vectors of length 2002.
My code works in python, but I don't know how to do the same thing in C++.
How can I convert my sequence of vectors into a sequence of LSTM-ready sequence of tensors?

Code:

/// concatenate vector 1 and vector 2 features
std::vector<float> vecOne_vecTwo_concat;
/// preallocate memory
vecOne_vecTwo_concat.reserve(vecOne.size() + vecTwo.size());
/// concatenate (first half is vecOne; second half is vecTwo)
/// add vecOne
vecOne_vecTwo_concat.insert(vecOne_vecTwo_concat.end(),
 vecOne.begin(), vecOne.end());
/// add vecTwo
vecOne_vecTwo_concat.insert(vecOne_vecTwo_concat.end(),
 vecTwo.begin(), vecTwo.end() );

/// append to vector of features
sequence_vector_.push_back(vecOne_vecTwo_concat);

/// check if we have enough sequences to make a classification
/// here n_rnn_steps_ is 10
/// each vector in this sequence is of length 2002
/// so we have 10 vectors, each of length 2002
if (sequence_vector_.size() == n_rnn_steps_) {

  /* Here we feed the concatenated vector sequence into
     the session running the LSTM graph
  */

  /// reset vector after we have feed it into the session
  sequence_vector_.clear();
}

You can have a direct access to the memory of a created tensor as a pointer.

Tensor aTensor(DT_FLOAT, TensorShape({ sequenceLength }));
float *p = aTensor.flat<float>().data();

Then you can copy your data to that memory or use memcpy.

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