Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Tensorflow Serving SavedModelBuilder Multi Version

From Leeroopedia
Knowledge Sources
Domains Model_Serialization, Version_Management
Last Updated 2026-02-13 17:00 GMT

Overview

Concrete tool for exporting multiple numbered model versions using TensorFlow's SavedModelBuilder, enabling version management in TensorFlow Serving.

Description

This is the same SavedModelBuilder API as used in basic model export, but applied in a version-management context. The --model_version flag in mnist_saved_model.py controls the version number, and running the export script multiple times with different version numbers produces the directory structure that TensorFlow Serving's filesystem source monitors.

The critical difference from single-version export is the operational pattern: multiple invocations with incrementing version numbers, potentially with different training parameters or data.

Usage

Use this when setting up a multi-version serving scenario. Run the export script multiple times with different --model_version values to create a versioned directory tree.

Code Reference

Source Location

  • Repository: tensorflow/serving
  • File: tensorflow_serving/example/mnist_saved_model.py
  • Lines: L49-155

Signature

# Same SavedModelBuilder API, with version from CLI flag
export_path = os.path.join(
    tf.compat.as_bytes(export_path_base),    # base directory (positional arg)
    tf.compat.as_bytes(str(FLAGS.model_version))  # version subdirectory
)
builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_path)
builder.add_meta_graph_and_variables(sess, tags, signature_def_map, main_op, strip_default_attrs)
builder.save()

Import

import tensorflow as tf
import os

I/O Contract

Inputs

Name Type Required Description
export_path_base str Yes Base directory for all model versions (positional CLI argument)
--model_version int No Version number (default 1); appended as subdirectory
--training_iteration int No Training iterations (default 1000); vary per version for different models

Outputs

Name Type Description
<base>/1/ Directory SavedModel for version 1
<base>/2/ Directory SavedModel for version 2
<base>/N/ Directory SavedModel for version N

Usage Examples

Export Multiple Versions

# Export version 1 with 1000 iterations
python tensorflow_serving/example/mnist_saved_model.py \
    --model_version=1 \
    --training_iteration=1000 \
    /tmp/mnist_model

# Export version 2 with 2000 iterations (improved model)
python tensorflow_serving/example/mnist_saved_model.py \
    --model_version=2 \
    --training_iteration=2000 \
    /tmp/mnist_model

# Result: /tmp/mnist_model/1/ and /tmp/mnist_model/2/ both exist
# TF Serving will discover both versions

# Verify directory structure
ls /tmp/mnist_model/
# Output: 1/  2/

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment