簡體   English   中英

在Tensorflow C ++中將浮點矢量傳遞給Tensor

[英]Pass Vector of Floats to Tensor in Tensorflow C++

我有一個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();
}

您可以直接訪問創建的張量的內存作為指針。

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

然后,您可以將數據復制到該內存或使用memcpy。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM