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 Tfjs Tf LoadLayersModel

From Leeroopedia


Summary

tf.loadLayersModel is the primary TensorFlow.js API for loading a pre-trained Keras-style layers model from a URL or custom I/O handler. It fetches the model topology (architecture) and binary weight files, deserializes them, and returns a fully reconstructed LayersModel ready for inference or further training.

API

tf.loadLayersModel(pathOrIOHandler: string|io.IOHandler, options?: io.LoadOptions): Promise<LayersModel>

Source

  • tfjs-layers/src/models.ts:L248-270

Type

API Doc

Signature

export async function loadLayersModel(
  pathOrIOHandler: string|io.IOHandler,
  options?: io.LoadOptions
): Promise<LayersModel>

// LoadOptions
// {
//   strict?: boolean (default true),
//   onProgress?: (fraction: number) => void
// }

Parameters

Parameter Type Required Description
pathOrIOHandler io.IOHandler Yes URL to the model.json file, or a custom IOHandler for loading from non-HTTP sources (e.g., local filesystem, IndexedDB)
options io.LoadOptions No Optional configuration for the loading process

LoadOptions

Option Type Default Description
strict boolean true If true, all model weights must match loaded weights exactly; mismatches throw an error
onProgress (fraction: number) => void undefined Callback invoked during weight download with a fraction (0 to 1) indicating progress

I/O

  • Inputs: URL to model.json or an IOHandler instance, plus optional load options
  • Outputs: Promise<LayersModel> with both model topology and weights fully loaded

Loading Process

The function performs the following steps internally:

  1. Parse the model.json to extract the model topology and weight manifest.
  2. Resolve binary weight shard URLs relative to the model.json location.
  3. Download all binary weight shards (potentially in parallel).
  4. Deserialize the binary data into typed arrays matching each weight's dtype and shape.
  5. Reconstruct the model architecture from the topology.
  6. Assign loaded weight values to the corresponding model variables.

Example

// Load pretrained GPT-2 weights
const model = await tf.loadLayersModel(
  'https://storage.googleapis.com/gpt2/model.json',
  {onProgress: (frac) => console.log(`${(frac*100).toFixed(0)}% loaded`)}
);

Implements

Principle:Tensorflow_Tfjs_Pretrained_Weight_Loading

Environment:Tensorflow_Tfjs_Browser_Runtime Heuristic:Tensorflow_Tfjs_Backend_Selection_Strategy

Domains

NLP Model_Loading

Sources

TensorFlow.js

Related Pages

Environments

Heuristics

Metadata

2026-02-10 00:00 GMT

Page Connections

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