简体   繁体   中英

How to save Tensorflow model using Tensorflow C-API

Using TF_GraphToGraphDef one can export a graph and using TF_GraphImportGraphDef one can import a Tensorflow graph. There also is a method TF_LoadSessionFromSavedModel which seems to offer loading of a Tensorflow model (ie graph including variables). But how does one save a Tensorflow model (graph including variables) using the C API?

Model saving in tensorflow is one of the worst programing experiences I have encountered. Never in my life have I been so frustrated with such horrible documentation I do not wish this to the worst of my enemies.

All actions in the C api are executed via the TF_SessionRun() function. This function has 12 arguments:

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

So what you want is to tell TF_SessionRun to execute an operation that will "save" the current model to a file.

The way I do it is by allocating a tensor and feeding it the name of the file to saves the model to. This saves the weights of the model, not sure if the model itself.

Here is an example execution of TF_SessionRun I know it's quite cryptic, I'll provide a whole script in a couple hours.

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);

This is an incomplete answer, I promise I will edit in a couple of hours,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM