tensorflow
keras
model
output nodes
neural networks

How do you get the name of the tensorflow output nodes in a Keras Model?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

When people ask for the output node name of a Keras model, they often mean one of several different things: the Keras output name, the symbolic tensor name, or the exported SavedModel signature key. Those values are related, but they are not always identical, so the first step is to decide which name your downstream tool actually needs.

Inspect Outputs Directly in Keras

For an in-memory Keras model, the simplest place to start is model.output_names and model.outputs.

python
1import tensorflow as tf
2
3inputs = tf.keras.Input(shape=(8,), name="features")
4x = tf.keras.layers.Dense(16, activation="relu", name="hidden")(inputs)
5outputs = tf.keras.layers.Dense(3, activation="softmax", name="class_probs")(x)
6
7model = tf.keras.Model(inputs=inputs, outputs=outputs)
8
9print("output_names:", model.output_names)
10for tensor in model.outputs:
11    print("tensor:", tensor.name)

model.output_names is usually the answer for high-level application code. It tells you the logical names Keras assigned to the outputs.

If the model has multiple outputs, the list shows them all:

python
1score = tf.keras.layers.Dense(1, name="score")(x)
2label = tf.keras.layers.Dense(3, activation="softmax", name="label")(x)
3multi_model = tf.keras.Model(inputs=inputs, outputs=[score, label])
4
5print(multi_model.output_names)

Inspect Graph-Level Tensor Names

Some tools expect graph tensor names rather than high-level Keras output names. In that case, create a concrete function and inspect its outputs.

python
1import tensorflow as tf
2
3@tf.function
4def infer(features):
5    return model(features)
6
7concrete = infer.get_concrete_function(
8    tf.TensorSpec(shape=[None, 8], dtype=tf.float32, name="features")
9)
10
11print("structured outputs:", concrete.structured_outputs)
12for output in concrete.outputs:
13    print("concrete tensor name:", output.name)

This is closer to what graph-based consumers see. The names here may include suffixes added by TensorFlow, so they are useful for inspection but not always ideal as long-term public contract names.

Verify Names After Export

If the model will be served or converted, treat the exported artifact as the source of truth. Export steps can change what the final client sees.

python
1import tensorflow as tf
2
3export_dir = "saved_model_dir"
4tf.saved_model.save(model, export_dir)
5
6loaded = tf.saved_model.load(export_dir)
7print(list(loaded.signatures.keys()))
8
9serving = loaded.signatures["serving_default"]
10print(serving.structured_outputs)

For serving systems, these signature keys are often more important than any symbolic node name from model construction time.

If you need a stable exported output key, define it explicitly:

python
1import tensorflow as tf
2
3@tf.function(input_signature=[tf.TensorSpec([None, 8], tf.float32, name="features")])
4def serve(features):
5    return {"prediction_score": model(features)}
6
7tf.saved_model.save(model, "explicit_model", signatures={"serving_default": serve})

Now the exported output name is the explicit key prediction_score, which is far easier to depend on than an autogenerated graph tensor name.

Common Pitfalls

The biggest mistake is assuming the Keras output name, tensor name, and SavedModel signature key are the same thing. They are often similar, but they serve different layers of the stack.

Another common issue is inspecting only the in-memory model and never checking the exported artifact. Production systems usually consume the exported artifact, not the notebook object that created it.

People also hardcode unstable tensor names with generated suffixes. If a downstream client needs a durable contract, define explicit serving signatures instead.

Finally, remember that output naming gets more complex with subclassed models and multi-output models. In those cases, inspection is safer than guessing.

If a converter or serving stack asks for a specific output name, add an integration check that loads the exported model and verifies the expected key before release.

Summary

  • Start with model.output_names and model.outputs for in-memory Keras inspection.
  • Use a concrete function when you need lower-level graph tensor names.
  • Inspect the SavedModel signatures if the model will be exported or served.
  • Define explicit signature keys when clients need stable output names.
  • Do not assume all "output node names" in TensorFlow refer to the same concept.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design