title: How to feed inputs into a loaded Tensorflow model using C++
body: <p>I want to create and train a model, export it and run inference in C++.</p>
  
  <p>I'm following the tutorial listed here: <a href="https://www.tensorflow.org/tutorials/wide_and_deep" rel="nofollow noreferrer">https://www.tensorflow.org/tutorials/wide_and_deep</a></p>
  
  <p>I'm also trying to use the <strong>SavedModel</strong> approach as described here since this is the canonical way to export TensorFlow graphs for serving:
  <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md" rel="nofollow noreferrer">https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md</a>.</p>
  
  <p>At the very end, I export the saved model as follows:</p>
  
  <pre><code>feature_spec = tf.contrib.layers.create_feature_spec_for_parsing(feature_columns)     
  serving_input_fn = input_fn_utils.build_parsing_serving_input_fn(feature_spec)    
  output = model.export_savedmodel(model_dir, serving_input_fn, as_text=True)                       
  print('Model saved to {}'.format(output))                                         
  </code></pre>
  
  <p>I see the saved_model.pbtxt has the following signature definition.</p>
  
  <pre><code>signature_def {
    key: "serving_default"
    value {
      inputs {
        key: "inputs"
        value {
          name: "input_example_tensor:0"
          dtype: DT_STRING
          tensor_shape {
            dim {
              size: -1
            }
          }
        }
      }
    outputs { 
    ...
  </code></pre>
  
  <p>I can load the saved model on the C++ side</p>
  
  <pre><code>SavedModelBundle bundle;                                        
  
  const std::string graph_path = "models/1498572863";            
  const std::unordered_set&lt;std::string&gt; tags = {"serve"};         
  
  Status status = LoadSavedModel(session_options,                        
                          run_options, graph_path,               
                          tags, &amp;bundle);                         
  </code></pre>
  
  <p>I'm stuck at the last part where I need to feed the input into this model. 
  The Run function expects the input parameter to be of the form: <code>std::vector&lt;std::pair&lt;string, Tensor&gt;&gt;</code>. </p>
  
  <p>I would have expected this to be a vector of pairs where the key is the feature name used in the python code and the Tensor is multiple values for that feature. </p>
  
  <p>However, it seems to expect the string to be "input_example_tensor". 
  I'm not sure how I'm supposed to now feed the model with different features using a single Tensor.</p>
  
  <pre><code>std::vector&lt;string&gt; output_tensor_names = {
  "binary_logistic_head/_classification_output_alternatives/classes_tensor"};
  
  // How do I create input_tensor?
  status = bundle.session-&gt;Run({{"input_example_tensor", input_tensor}}
      output_tensor_names, {}, &amp;outputs);          
  </code></pre>
  
  <p><strong>Solution</strong></p>
  
  <p>I did something like this</p>
  
  <pre><code>tensorflow::Example example;
  auto&amp; tf_feature_map = *(example.mutable_features()-&gt;mutable_feature());
  
  tf_feature_map["name"].mutable_int64_list()-&gt;add_value(15);
  const std::string&amp; serialized = example.SerializeAsString();
  tensorflow::Input input({serialized});
  
  status = bundle.session-&gt;Run({{"input_example_tensor", input.tensor()}}
      output_tensor_names, {}, &amp;outputs);
  </code></pre>