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.

Implementation:Tensorflow Tfjs Models Test

From Leeroopedia


Knowledge Sources
Domains Testing, Layers_API
Last Updated 2026-02-10 06:00 GMT

Overview

This is one of the largest test suites (3236 lines), providing comprehensive validation of model construction, serialization, deserialization, and the Sequential API in TensorFlow.js Layers. It covers nested model topologies (Sequential within Sequential, Functional within Sequential), model deserialization from JSON (modelFromJSON) including complex architectures from Python Keras, URL-based model loading (loadLayersModel), save-load round-trips with optimizer state preservation, IOHandler-based loading, and the full Sequential model API including add(), pop(), predict(), fit(), evaluate(), and getLayer().

Code Reference

Source Location: tfjs-layers/src/models_test.ts (3236 lines)

Repository: GitHub

Test Describe Blocks

  • Nested model topology - Multi-level nesting:
    • Sequential as first layer, Functional as first layer
    • Sequential as second layer
    • Multiple levels of nesting with training verification
  • modelFromJSON - Deserializing models from JSON configs:
    • Simple Sequential and Functional models
    • Models with activations, regularizers, BatchNormalization
    • MobileNet-like architectures
    • Model with missing optimizer config
  • loadLayersModel from URL - HTTP-based model loading:
    • Default and custom weight URL conversion
    • Loading models with different weight file structures
    • Strict mode for weight loading
    • Request init and headers
  • Saving+loading model with optimizer - Optimizer state preservation:
    • SGD, Adam, RMSProp, Adagrad optimizer states
    • Continued training after load
  • loadLayersModel from IOHandler - Loading through custom IO handlers
  • Sequential - Full Sequential model API:
    • Construction with and without config
    • add() with layers and models
    • pop() to remove layers
    • predict(), fit(), evaluate()
    • Symbolic and concrete tensor operations
    • Weight management
    • getLayer() by name and index
    • Serialization (toJSON, getConfig)
  • Functional-model saving and loading - Functional model serialization round-trips

I/O Contract

Inputs to tests:

  • Model architectures: Sequential, Functional, nested
  • JSON model configurations matching Python Keras output
  • URL endpoints for model topology and weight files
  • IO handler implementations with load() methods
  • Training data tensors for fit() and evaluate()

Expected outputs/assertions:

  • Nested models produce consistent predictions at all nesting levels
  • Deserialized models have correct layer types, names, and configurations
  • Loaded models produce identical predictions to saved models
  • Optimizer state is correctly preserved across save/load
  • Sequential add()/pop() correctly modifies model topology
  • getLayer() returns correct layer by name or index
  • Weight count and shapes match expected values

Usage Example

describeMathCPU('Nested model topology', () => {
  it('Nested Sequential model: Sequential as first layer', done => {
    const modelLevel1 = tfl.sequential(
        {layers: [tfl.layers.dense({units: 2, inputShape: [3]})]});
    const x = ones([1, 3]);
    const y = modelLevel1.predict(x) as Tensor;

    const modelLevel2 = tfl.sequential();
    modelLevel2.add(modelLevel1);
    expectTensorsClose(modelLevel2.predict(x) as Tensor, y);

    const modelLevel3 = tfl.sequential();
    modelLevel3.add(modelLevel2);
    expectTensorsClose(modelLevel3.predict(x) as Tensor, y);
    done();
  });
});

Test Coverage Summary

Category Count Details
Nested Models 5+ Multi-level Sequential and Functional nesting
modelFromJSON 15+ Various architectures including MobileNet-like
loadLayersModel (URL) 20+ URL conversion, weight files, strict mode
Optimizer Persistence 10+ SGD, Adam, RMSProp, Adagrad state
Sequential API 30+ add, pop, predict, fit, evaluate, getLayer, toJSON
Functional Save/Load 5+ Round-trip serialization
Test Environment Mixed CPU, GPU, WebGL2

Related Pages

Page Connections

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