tfjs
machine learning
deep learning
TensorFlow.js
model comparison

Difference between tfjs_layers_model and tfjs_graph_model

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In TensorFlow.js, tf.LayersModel and tf.GraphModel are both usable model types, but they come from different sources and support different workflows. The most useful distinction is this: LayersModel fits the Keras-style layers API, while GraphModel represents a prebuilt computation graph, usually loaded for inference.

If you are choosing between them, start by asking where the model came from and whether you need to train it in JavaScript. Those two questions usually decide the answer quickly.

LayersModel Is the Keras-Style API

A LayersModel is what you get from the TensorFlow.js layers API, including sequential and functional-style models. It supports familiar high-level methods such as compile, fit, evaluate, and predict.

javascript
1import * as tf from "@tensorflow/tfjs";
2
3const model = tf.sequential();
4model.add(tf.layers.dense({ units: 8, activation: "relu", inputShape: [2] }));
5model.add(tf.layers.dense({ units: 1 }));
6
7model.compile({
8  optimizer: "sgd",
9  loss: "meanSquaredError",
10});
11
12const xs = tf.tensor2d([[0, 0], [0, 1], [1, 0], [1, 1]]);
13const ys = tf.tensor2d([[0], [1], [1], [2]]);
14
15await model.fit(xs, ys, { epochs: 20, verbose: 0 });
16const output = model.predict(tf.tensor2d([[1, 1]]));
17output.print();

That workflow is ideal when you want to define, train, fine-tune, or inspect a model in JavaScript itself.

GraphModel Is for Executing a Prebuilt Graph

A GraphModel is typically loaded from a TensorFlow SavedModel conversion or another graph-based export. It is better viewed as an executable inference graph than as a trainable layers object.

javascript
1import * as tf from "@tensorflow/tfjs-node";
2
3async function run() {
4  const model = await tf.loadGraphModel("file://./graph-model/model.json");
5  const input = tf.zeros([1, 224, 224, 3]);
6  const result = model.predict(input);
7
8  if (Array.isArray(result)) {
9    console.log(result.length);
10  } else {
11    console.log(result.shape);
12  }
13}
14
15run();

The code above assumes you already have converted graph-model files on disk. That is typical usage: GraphModel is loaded, then executed for inference.

Choose Based on Workflow, Not Just File Names

The difference is not only about serialization format. It is about what you plan to do next.

Choose LayersModel when you want:

  • model construction with the layers API
  • 'compile and fit'
  • easy transfer from Keras-style design
  • direct layer inspection and manipulation

Choose GraphModel when you want:

  • inference from a converted TensorFlow graph
  • access to graph-based exported models
  • execution without the training-oriented layers workflow

This is why many browser demos that fine-tune small models use LayersModel, while many production inference bundles load GraphModel.

Training Support Is a Practical Divider

In day-to-day work, the biggest practical divider is training. LayersModel is designed for the higher-level training loop. GraphModel is mostly an inference target and does not give you the same training API surface.

That means a graph model may be perfect for shipping a pretrained classifier to the browser, but it is the wrong choice if your application needs to call fit on user data in the client.

Loading APIs Are Different Too

TensorFlow.js makes the distinction visible in its loading functions:

  • 'tf.loadLayersModel(...)'
  • 'tf.loadGraphModel(...)'

That split is a hint that you should not treat the two model types as interchangeable wrappers around the same behavior. They overlap in prediction use cases, but their capabilities and intended sources differ.

Common Pitfalls

The most common mistake is loading a GraphModel and expecting Keras-like training methods such as compile and fit.

Another mistake is choosing a LayersModel just because the API feels more familiar, even though the real requirement is to run a converted SavedModel for inference.

It is also easy to talk about them as if they differ only in file format. The more important difference is the programming model and supported workflow after loading.

Finally, always check the model export path you already have. The right TensorFlow.js model type is often determined by the upstream conversion process, not just local preference.

Summary

  • 'tf.LayersModel is the Keras-style TensorFlow.js model type.'
  • 'tf.GraphModel is usually a loaded inference graph from converted TensorFlow assets.'
  • Use LayersModel when you need training-oriented methods such as fit.
  • Use GraphModel when you need to execute a prebuilt graph for inference.
  • The correct choice depends on model origin and workflow, not just naming.

Course illustration
Course illustration

All Rights Reserved.