在Tensorflow C ++中将Floats的向量传递给Tensor

时间:2018-05-08 23:00:38

标签: python c++ tensorflow lstm

我有一个LSTM冻结图,它有10个序列,每个输入序列有10个长度为2002的向量 我的代码在python中工作,但我不知道如何在C ++中做同样的事情 如何将我的载体序列转换为LSTM就绪序列的张量序列?

代码:

/// 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();
}

1 个答案:

答案 0 :(得分:0)

您可以直接访问创建的张量的内存作为指针。

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

然后,您可以将数据复制到该内存或使用memcpy。