如何保存RNN的状态以从TF中的图中提交
How do I save the states of an RNN to file from a Graph in TF?
以下代码来自TensorFlow服务API:
// Implementation of Predict using the SavedModel SignatureDef format.
Status SavedModelPredict(const RunOptions& run_options, ServerCore* core,
const PredictRequest& request,
PredictResponse* response) {
// Validate signatures.
ServableHandle<SavedModelBundle> bundle;
TF_RETURN_IF_ERROR(core->GetServableHandle(request.model_spec(), &bundle));
const string signature_name = request.model_spec().signature_name().empty()
? kDefaultServingSignatureDefKey
: request.model_spec().signature_name();
auto iter = bundle->meta_graph_def.signature_def().find(signature_name);
if (iter == bundle->meta_graph_def.signature_def().end()) {
return errors::FailedPrecondition(strings::StrCat(
"Serving signature key "", signature_name, "" not found."));
}
SignatureDef signature = iter->second;
MakeModelSpec(request.model_spec().name(), signature_name,
bundle.id().version, response->mutable_model_spec());
std::vector<std::pair<string, Tensor>> input_tensors;
std::vector<string> output_tensor_names;
std::vector<string> output_tensor_aliases;
TF_RETURN_IF_ERROR(PreProcessPrediction(signature, request, &input_tensors,
&output_tensor_names,
&output_tensor_aliases));
std::vector<Tensor> outputs;
RunMetadata run_metadata;
TF_RETURN_IF_ERROR(bundle->session->Run(run_options, input_tensors,
output_tensor_names, {}, &outputs,
&run_metadata));
return PostProcessPredictionResult(signature, output_tensor_aliases, outputs,
response);
}
此代码使用存储的模型运行预测。就我而言,该存储的模型是RNN。
在实际运行的以下行之后:
TF_RETURN_IF_ERROR(bundle->session->Run(run_options, input_tensors,
output_tensor_names, {}, &outputs,
&run_metadata));
我想将RNN的状态保存到文件/内存,以便在每个预测后以后访问它们。我认为可以通过变量访问这些状态:
bundle->meta_graph_def
但不清楚如何确切访问RNN的状态值,然后将它们保存到文件中。
您必须通过会话而不是事实来获得值。在你的行中
bundle->session->Run(run_options, input_tensors,
output_tensor_names, {}, &outputs,
&run_metadata)
您不仅应该请求预测的结果,还应要求状态张量。请注意,该状态的值未在 session->Run
的特定调用之后存储,仅存储变量,rnn的状态是计算值并在返回结果后删除。
我不使用C 接口,所以请原谅我在Python中提供代码示例,希望它仍然有用(我敢打赌,这不是您第一次必须忍受阅读Python示例(,在Python中,这看起来像:
prediction, state = sess.run([prediction_tensor, state_tensor], feed_dict=...)
相关文章:
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- Python中的for循环与C++有何不同
- 在C++中将类(带有Vector成员)保存为二进制文件
- C++.我想将更改后的特定字符行保存在字符串中
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 哈希文件递归并保存到矢量Cryptopp中
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 无法通过头文件保存变量的值
- 我们可以将数据永久保存为数据结构吗?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 保存/加载大量短数组到二进制文件
- Gstreamer:每 5 秒使用多文件墨水保存图像/jpeg
- 如何保存RNN的状态以从TF中的图中提交