class_docstr: Builds the `SavedModel` protocol buffer and saves variables and assets.
  
  The `SavedModelBuilder` class provides the functionality to build a
  `SavedModel` protocol buffer. Specifically, this allows multiple meta
  graphs to be saved as part of a single language-neutral `SavedModel`,
  while sharing variables and assets.
  
  To build a SavedModel, the first meta graph must be saved with variables.
  Subsequent meta graphs will simply be saved with their graph definitions. If
  assets need to be saved and written or copied to disk, they can be provided
  when the meta graph def is added. If multiple meta graph defs are associated
  an asset of the same name, only the first version is retained.
  
  Each meta graph added to the SavedModel must be annotated with tags. The tags
  provide a means to identify the specific meta graph to load and restore, along
  with the shared set of variables and assets.
  
  Typical usage for the `SavedModelBuilder`:
  
  ```python
  ...
  builder = tf.compat.v1.saved_model.Builder(export_dir)
  
  with tf.compat.v1.Session(graph=tf.Graph()) as sess:
    ...
    builder.add_meta_graph_and_variables(sess,
                                    ["foo-tag"],
                                    signature_def_map=foo_signatures,
                                    assets_list=foo_assets)
  ...
  
  with tf.compat.v1.Session(graph=tf.Graph()) as sess:
    ...
    builder.add_meta_graph(["bar-tag", "baz-tag"])
  ...
  
  builder.save()
  ```
  
  Note: This function will only be available through the v1 compatibility
  library as tf.compat.v1.saved_model.builder.SavedModelBuilder or
  tf.compat.v1.saved_model.Builder. Tensorflow 2.0 will introduce a new
  object-based method of creating SavedModels.
class_docstr_tok: ["Builds","the","`","SavedModel","`","protocol","buffer","and","saves","variables","and","assets",".","The","`","SavedModelBuilder","`","class","provides","the","functionality","to","build","a","`","SavedModel","`","protocol","buffer",".","Specifically",",","this","allows","multiple","meta","graphs","to","be","saved","as","part","of","a","single","language-neutral","`","SavedModel","`",",","while","sharing","variables","and","assets",".","To","build","a","SavedModel",",","the","first","meta","graph","must","be","saved","with","variables",".","Subsequent","meta","graphs","will","simply","be","saved","with","their","graph","definitions",".","If","assets","need","to","be","saved","and","written","or","copied","to","disk",",","they","can","be","provided","when","the","meta","graph","def","is","added",".","If","multiple","meta","graph","defs","are","associated","an","asset","of","the","same","name",",","only","the","first","version","is","retained",".","Each","meta","graph","added","to","the","SavedModel","must","be","annotated","with","tags",".","The","tags","provide","a","means","to","identify","the","specific","meta","graph","to","load","and","restore",",","along","with","the","shared","set","of","variables","and","assets",".","Typical","usage","for","the","`","SavedModelBuilder","`",":","``","`","python","...","builder","=","tf.compat.v1.saved_model.Builder","(","export_dir",")","with","tf.compat.v1.Session","(","graph=tf.Graph","(",")",")","as","sess",":","...","builder.add_meta_graph_and_variables","(","sess",",","[","``","foo-tag","''","]",",","signature_def_map=foo_signatures",",","assets_list=foo_assets",")","...","with","tf.compat.v1.Session","(","graph=tf.Graph","(",")",")","as","sess",":","...","builder.add_meta_graph","(","[","``","bar-tag","''",",","``","baz-tag","''","]",")","...","builder.save","(",")","``","`","Note",":","This","function","will","only","be","available","through","the","v1","compatibility","library","as","tf.compat.v1.saved_model.builder.SavedModelBuilder","or","tf.compat.v1.saved_model.Builder",".","Tensorflow","2.0","will","introduce","a","new","object-based","method","of","creating","SavedModels","."]
docstr: Adds the current meta graph to the SavedModel and saves variables.
  
  Creates a Saver to save the variables from the provided session. Exports the
  corresponding meta graph def. This function assumes that the variables to be
  saved have been initialized. For a given `SavedModelBuilder`, this API must
  be called exactly once and for the first meta graph to save. For subsequent
  meta graph defs to be added, the `add_meta_graph()` API must be used.
  
  Args:
    sess: The TensorFlow session from which to save the meta graph and
      variables.
    tags: The set of tags with which to save the meta graph.
    signature_def_map: The map of signature def map to add to the meta graph
      def.
    assets_list: Assets to be saved with SavedModel.
    clear_devices: Set to true if the device info on the default graph should
      be cleared.
    init_op: Op or group of ops to execute when the graph is loaded. Note
        that when the init_op is specified it is run after the restore op at
      load-time.
    train_op: Op or group of ops that trains the model when run. This will
      not be run automatically when the graph is loaded, instead saved in
      a SignatureDef accessible through the exported MetaGraph.
    strip_default_attrs: Boolean. If `True`, default-valued attributes will be
      removed from the NodeDefs. For a detailed guide, see
      [Stripping Default-Valued Attributes](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md#stripping-default-valued-attributes).
    saver: An instance of tf.compat.v1.train.Saver that will be used to export the
      metagraph and save variables. If None, a sharded Saver that restores
      all variables will be used.
docstr_tok: ["Adds","the","current","meta","graph","to","the","SavedModel","and","saves","variables",".","Creates","a","Saver","to","save","the","variables","from","the","provided","session",".","Exports","the","corresponding","meta","graph","def",".","This","function","assumes","that","the","variables","to","be","saved","have","been","initialized",".","For","a","given","`","SavedModelBuilder","`",",","this","API","must","be","called","exactly","once","and","for","the","first","meta","graph","to","save",".","For","subsequent","meta","graph","defs","to","be","added",",","the","`","add_meta_graph","(",")","`","API","must","be","used",".","Args",":","sess",":","The","TensorFlow","session","from","which","to","save","the","meta","graph","and","variables",".","tags",":","The","set","of","tags","with","which","to","save","the","meta","graph",".","signature_def_map",":","The","map","of","signature","def","map","to","add","to","the","meta","graph","def",".","assets_list",":","Assets","to","be","saved","with","SavedModel",".","clear_devices",":","Set","to","true","if","the","device","info","on","the","default","graph","should","be","cleared",".","init_op",":","Op","or","group","of","ops","to","execute","when","the","graph","is","loaded",".","Note","that","when","the","init_op","is","specified","it","is","run","after","the","restore","op","at","load-time",".","train_op",":","Op","or","group","of","ops","that","trains","the","model","when","run",".","This","will","not","be","run","automatically","when","the","graph","is","loaded",",","instead","saved","in","a","SignatureDef","accessible","through","the","exported","MetaGraph",".","strip_default_attrs",":","Boolean",".","If","`","True","`",",","default-valued","attributes","will","be","removed","from","the","NodeDefs",".","For","a","detailed","guide",",","see","[","Stripping","Default-Valued","Attributes","]","(","https",":","\/\/github.com\/tensorflow\/tensorflow\/blob\/master\/tensorflow\/python\/saved_model\/README.md","#","stripping-default-valued-attributes",")",".","saver",":","An","instance","of","tf.compat.v1.train.Saver","that","will","be","used","to","export","the","metagraph","and","save","variables",".","If","None",",","a","sharded","Saver","that","restores","all","variables","will","be","used","."]
code: def add_meta_graph_and_variables(
      self,
      sess,
      tags,
      signature_def_map=None,
      assets_list=None,
      clear_devices=False,
      init_op=None,
      train_op=None,
      strip_default_attrs=False,
      saver=None,
  ):
      # pylint: disable=line-too-long
      """Adds the current meta graph to the SavedModel and saves variables.
  
      Creates a Saver to save the variables from the provided session. Exports the
      corresponding meta graph def. This function assumes that the variables to be
      saved have been initialized. For a given `SavedModelBuilder`, this API must
      be called exactly once and for the first meta graph to save. For subsequent
      meta graph defs to be added, the `add_meta_graph()` API must be used.
  
      Args:
        sess: The TensorFlow session from which to save the meta graph and
          variables.
        tags: The set of tags with which to save the meta graph.
        signature_def_map: The map of signature def map to add to the meta graph
          def.
        assets_list: Assets to be saved with SavedModel.
        clear_devices: Set to true if the device info on the default graph should
          be cleared.
        init_op: Op or group of ops to execute when the graph is loaded. Note
            that when the init_op is specified it is run after the restore op at
          load-time.
        train_op: Op or group of ops that trains the model when run. This will
          not be run automatically when the graph is loaded, instead saved in
          a SignatureDef accessible through the exported MetaGraph.
        strip_default_attrs: Boolean. If `True`, default-valued attributes will be
          removed from the NodeDefs. For a detailed guide, see
          [Stripping Default-Valued Attributes](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md#stripping-default-valued-attributes).
        saver: An instance of tf.compat.v1.train.Saver that will be used to export the
          metagraph and save variables. If None, a sharded Saver that restores
          all variables will be used.
  
      """
      # pylint: enable=line-too-long
      if self._has_saved_variables:
          raise AssertionError(
              "Graph state including variables and assets has "
              "already been saved. Please invoke "
              "`add_meta_graph()` instead."
          )
  
      # Validate the signature def map to ensure all included TensorInfos are
      # properly populated.
      signature_def_map = signature_def_map or {}
      self._validate_signature_def_map(signature_def_map)
  
      # Create a SignatureDef pointing to the graph initialization op, which will
      # be added to the MetaGraphDef.
      _add_op_to_signature_def_map(
          signature_def_map, init_op, constants.INIT_OP_SIGNATURE_KEY
      )
      _add_op_to_signature_def_map(
          signature_def_map, train_op, constants.TRAIN_OP_SIGNATURE_KEY
      )
  
      path_helpers.get_or_create_variables_dir(self._export_dir)
      variables_path = path_helpers.get_variables_path(self._export_dir)
  
      saver = self._maybe_create_saver(saver)
  
      # Save the variables. Also, disable writing the checkpoint state proto. The
      # file is not used during SavedModel loading. In addition, since a
      # SavedModel can be copied or moved, this avoids the checkpoint state to
      # become outdated.
      saver.save(
          sess,
          variables_path,
          write_meta_graph=False,
          write_state=False,
      )
  
      # Export the meta graph def.
  
      # The graph almost certainly previously contained at least one Saver, and
      # possibly several (e.g. one for loading a pretrained embedding, and another
      # for the model weights).  Removing the preexisting ones was the
      # motivation for the clear_extraneous_savers option, but it turns out that
      # there are edge cases where that option breaks the graph.  Until that is
      # resolved, we just leave the option set to False for now.
      # TODO(soergel): Reinstate clear_extraneous_savers=True when possible.
      meta_graph_def = saver.export_meta_graph(
          clear_devices=clear_devices,
          strip_default_attrs=strip_default_attrs,
      )
  
      # Save asset files and write them to disk, if any.
      self._save_and_write_assets(meta_graph_def, assets_list)
  
      # Tag the meta graph def and add it to the SavedModel.
      self._tag_and_add_meta_graph(
          meta_graph_def, tags, signature_def_map
      )
  
      # Mark this instance of SavedModel as having saved variables, such that
      # subsequent attempts to save variables will fail.
      self._has_saved_variables = True
  
imports: import functools
  import os
  from google.protobuf.any_pb2 import Any
  from tensorflow.core.framework import types_pb2
  from tensorflow.core.protobuf import meta_graph_pb2
  from tensorflow.core.protobuf import saved_model_pb2
  from tensorflow.core.protobuf import saver_pb2
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import tensor
  from tensorflow.python.lib.io import file_io
  from tensorflow.python.ops import variables
  from tensorflow.python.platform import tf_logging
  from tensorflow.python.saved_model import fingerprinting_utils
  from tensorflow.python.saved_model import path_helpers
  from tensorflow.python.saved_model import signature_def_utils
  from tensorflow.python.saved_model.pywrap_saved_model import constants
  from tensorflow.python.saved_model.pywrap_saved_model import metrics
  from tensorflow.python.training import saver
  from tensorflow.python.util import compat
  from tensorflow.python.util.deprecation import deprecated_args
  from tensorflow.python.util.tf_export import tf_export
label_desc: Use image node_id 8 for calling the _SavedModelBuilder obj's underlying member method code with example usage: obj.add_meta_graph_and_variables(sess, tags, signature_def_map, assets_list, clear_devices, init_op, train_op, strip_default_attrs, saver) without return types