Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Workflow:Tensorflow Tfjs Transfer Learning

From Leeroopedia
Revision as of 11:03, 16 February 2026 by Admin (talk | contribs) (Auto-imported from workflows/Tensorflow_Tfjs_Transfer_Learning.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Transfer_Learning, Fine_Tuning, Browser_ML
Last Updated 2026-02-10 06:00 GMT

Overview

End-to-end process for loading a pretrained TensorFlow.js model, modifying its architecture for a new task, and retraining on custom data in the browser or Node.js.

Description

This workflow enables adaptation of pretrained models to new tasks by leveraging learned feature representations. A base model is loaded (either converted from Python or previously saved in TensorFlow.js format), its layers are selectively frozen to preserve pretrained knowledge, new task-specific layers are added on top using the Functional API, and the combined model is retrained on a smaller domain-specific dataset. This approach requires significantly less training data and compute than training from scratch.

Usage

Execute this workflow when you have a pretrained model (e.g., MobileNet for image features, or any SavedModel converted to TensorFlow.js) and want to adapt it to a new classification, regression, or feature extraction task using a small amount of domain-specific data collected in the browser or provided locally.

Execution Steps

Step 1: Load the Base Model

Load a pretrained model using tf.loadLayersModel (for Keras-style models) or tf.loadGraphModel (for SavedModel conversions). For transfer learning with layer manipulation, a Layers model is required. The base model provides learned feature representations from its original training task.

Key considerations:

  • Layers models allow access to intermediate layers and weight freezing
  • Graph models are inference-only and cannot be retrained directly; extract features and build a new Layers model on top
  • Loading from a URL fetches model.json and weight shards over HTTP

Step 2: Select a Feature Extraction Layer

Identify an intermediate layer in the base model whose output serves as a useful feature representation for the new task. Typically this is a layer near the end of the network but before the final classification head. Create a new model that maps from the base model's input to this intermediate layer's output.

Key considerations:

  • Earlier layers capture low-level features (edges, textures); later layers capture high-level semantics
  • Use model.getLayer(name) or model.layers to inspect the architecture
  • The truncated model becomes the feature extractor whose output feeds into the new head

Step 3: Freeze Base Model Weights

Set trainable = false on the base model's layers to prevent their weights from being updated during retraining. This preserves the pretrained feature representations and limits training to only the new task-specific layers, reducing compute requirements and the risk of overfitting on small datasets.

Key considerations:

  • Freezing is applied per-layer via the trainable property
  • All layers in the base model should typically be frozen for small datasets
  • For larger datasets, unfreezing the last few layers (fine-tuning) can improve results

Step 4: Add New Task Layers

Build new layers on top of the frozen feature extractor using the Functional API (tf.model) or by creating a new Sequential model that accepts the feature extractor's output shape. Common additions include a global average pooling layer, one or more dense layers, and a final output layer matching the new task's requirements.

Key considerations:

  • The new head's input shape must match the feature extractor's output shape
  • For classification, the final dense layer uses softmax activation with units equal to the number of classes
  • For regression, the final dense layer uses linear activation with units matching the output dimensions
  • Use tf.model({ inputs, outputs }) to create the combined model

Step 5: Compile and Train

Compile the combined model with an optimizer, loss function, and metrics appropriate for the new task, then train using fit or fitDataset. Because most weights are frozen, training is fast and requires fewer examples. Use callbacks like EarlyStopping to prevent overfitting.

Key considerations:

  • Use a lower learning rate than training from scratch to avoid disrupting pretrained features
  • EarlyStopping callback monitors validation loss and halts training when it stops improving
  • Class weights can address imbalanced datasets
  • validationSplit or separate validation data helps monitor generalization

Step 6: Evaluate and Deploy

Evaluate the retrained model on held-out test data, then save it for deployment. The saved model includes both the frozen pretrained weights and the newly trained task-specific weights as a single artifact.

Key considerations:

  • The saved model is self-contained and can be loaded without the original base model
  • Save to IndexedDB for browser persistence or to a server for sharing across clients
  • The retrained model can be further fine-tuned later by unfreezing base layers

Execution Diagram

GitHub URL

Workflow Repository