python - 在 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。
推荐阅读
- c++ - std::greater{} 和 std::greater 之间有什么区别
()? - android - Kotlin Android 创建新目录
- python - Pandas 函数向量化
- java - 如何使用 Java/Kotlin 使用 R 语言脚本/程序或将其转换为 Android 应用程序?
- python-2.7 - 无法使用drpbox API v2 python下载文件
- javascript - Webpack 错误:configuration.module.rules[0] 有一个未知属性 'query'
- javascript - 矩形到达某处时的Javascript显示代码
- wpf - 如何处理点击事件然后传递给WPF中的底层应用程序
- c# - Delphi 到 C# 通过(未注册的)COM 互操作性示例?
- javascript - 如何为样式组件的组合制作接口?