ML Community Day is November 9! Join us for updates from TensorFlow, JAX, and more Learn more


Builds the SavedModel protocol buffer and saves variables and assets.

Used in the notebooks

Used in the guide

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:

builder = tf.compat.v1.saved_model.Builder(export_dir)

with tf.compat.v1.Session(graph=tf.Graph()) as sess:

with tf.compat.v1.Session(graph=tf.Graph()) as sess:
  builder.add_meta_graph(["bar-tag", "baz-tag"])



View source

Adds the current meta graph to the SavedModel.

Creates a Saver in the current scope and uses the Saver to export the meta graph def. Invoking this API requires the add_meta_graph_and_variables() API to have been invoked before.

tags The set of tags to annotate the meta graph def with.
signature_def_map The map of signature defs to be added to the meta graph def.
assets_collection Assets to be saved with SavedModel. Note that this list should be a subset of the assets saved as part of the first meta graph in the 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 opts 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.
saver An instance of tf.compat.v1.train.Saver that will be used to export the metagraph. If None, a sharde