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 Model Save Test

From Leeroopedia


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

Overview

This test suite validates the model saving functionality in TensorFlow.js Layers, specifically the LayersModel.save() method. It tests saving model topology, weights, and metadata through custom IO handlers, verifying that the saved artifacts include correct format identifiers, version information, weight specifications (shapes, dtypes, names), and model topology JSON. The suite also tests save options like trainable-only weight saving, error handling for missing save methods, and complete save-load round-trip integrity for both Sequential and Functional models.

Code Reference

Source Location: tfjs-layers/src/model_save_test.ts (579 lines)

Repository: GitHub

Test Describe Blocks

  • LayersModel.save - Core saving tests:
    • Meta-information (format, generatedBy, convertedBy) for Sequential and Functional models
    • Saving all weights (topology, weight specs, shapes, dtypes)
    • Saving only trainable weights
    • Error handling for handler without save method
    • Error handling for invalid save URL
    • Model topology verification
    • Weight data binary buffer verification
  • Save-load round trips - End-to-end save and reload verification:
    • Sequential model round-trip
    • Functional model round-trip
    • Models with non-trainable layers
    • Weight value integrity after round-trip

I/O Contract

Inputs to tests:

  • Sequential and Functional models with Dense layers
  • Custom IOHandler implementations that capture saved artifacts
  • Save options: {trainableOnly: true}

Expected outputs/assertions:

  • savedArtifacts.format equals 'layers-model'
  • savedArtifacts.generatedBy contains TensorFlow.js version string
  • Weight specs have correct names (containing '/kernel' or '/bias'), shapes, and dtypes
  • trainableOnly option excludes non-trainable layer weights
  • Missing save method throws appropriate error
  • Round-trip: reloaded model produces identical predictions to original

Usage Example

describeMathCPUAndGPU('LayersModel.save', () => {
  class IOHandlerForTest implements io.IOHandler {
    savedArtifacts: io.ModelArtifacts;
    async save(modelArtifacts: io.ModelArtifacts): Promise<io.SaveResult> {
      this.savedArtifacts = modelArtifacts;
      return {modelArtifactsInfo: null};
    }
  }

  it('Saving all weights succeeds', async () => {
    const model = tfl.sequential();
    model.add(tfl.layers.dense({units: 3, inputShape: [5]}));
    const handler = new IOHandlerForTest();
    await model.save(handler);
    expect(handler.savedArtifacts.weightSpecs.length).toEqual(2);
    expect(handler.savedArtifacts.weightSpecs[0].shape).toEqual([5, 3]);
  });
});

Test Coverage Summary

Category Count Details
Meta-Information 3 Format, generatedBy, convertedBy
Weight Saving 4+ All weights, trainable-only, specs verification
Error Handling 2+ Missing save method, invalid URL
Save-Load Round Trip 5+ Sequential, Functional, weight integrity
Test Environment Mixed CPU, GPU, WebGL2

Related Pages

Page Connections

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