簡體   English   中英

如何使用Tensorflow C-API保存Tensorflow模型

[英]How to save Tensorflow model using Tensorflow C-API

使用TF_GraphToGraphDef可以導出圖,使用TF_GraphImportGraphDef可以導入Tensorflow圖。 還有一個方法TF_LoadSessionFromSavedModel似乎提供了Tensorflow模型的加載(即,包括變量的圖形)。 但是,如何使用C API保存Tensorflow模型(包含變量的圖形)呢?

張量流中的模型保存是我遇到的最糟糕的編程經驗之一。 我一生中從來沒有對如此可怕的文檔感到沮喪,我不希望這對我的敵人最不利。

C api中的所有操作都通過TF_SessionRun()函數執行。 此函數有12個參數:

TF_CAPI_EXPORT extern void TF_SessionRun(
  TF_Session *session,            // Pointer to a TF session
  const TF_Buffer *run_options,   // No clue what this does
  const TF_Output *inputs,        // Your model inputs (not the actual input data)
  TF_Tensor* const* input_values, // Your input tensors (the actual data)
  int ninputs,                    // Number of inputs for a given operation (operations will be clear in a bit)
  const TF_Output* outputs,       // Your model outputs (not the actual output data)
  TF_Tensor** output_values,      // Your output tensors (the actual data)
  int noutputs,                   // Number of inputs for a given operation

  const TF_Operation* const* target_opers, // Your model operation (the actual computation to be performed for example training(fitting), computing metric, saving)
  int ntargets,                            // Number of targets (in case of multi output models for example)
  TF_Buffer* run_metadata,        // Absolutely no clue what this is
  TF_Status*);                    // Model status for when all fails with some cryptic error no one will help you debug

因此,您想要的是告訴TF_SessionRun執行將當前模型“保存”到文件的操作。

我這樣做的方法是分配張量並將其輸入文件名以保存模型。 這樣可以節省模型的權重,無需確定模型本身是否合適。

這是TF_SessionRun的示例執行,我知道它很神秘,我將在幾個小時內提供整個腳本。

TF_Output inputs[1] = {model->checkpoint_file}; // Input
  TF_Tensor* t = Belly_ScalarStringTensor(str, model->status); // This does the tensor allocation with the output filename
  TF_Tensor* input_values[1] = {t}; // Input data, the actual tensor
  //TF_Operation* op[1] = {model->save_op}; // Tha "save" operation
  // Run and pray
  TF_SessionRun(model->session,
                NULL,
                inputs, input_values, 1,
                /* No outputs */
                NULL, NULL, 0,
                /* The operation */
                op, 1,
                NULL,
                model->status);
  TF_DeleteTensor(t);

這是一個不完整的答案,我保證我會在幾個小時內進行編輯,

暫無
暫無

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

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