Implementation:Tensorflow Tfjs Model Save Test
| 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
IOHandlerimplementations that capture saved artifacts - Save options:
{trainableOnly: true}
Expected outputs/assertions:
savedArtifacts.formatequals'layers-model'savedArtifacts.generatedBycontains TensorFlow.js version string- Weight specs have correct names (containing '/kernel' or '/bias'), shapes, and dtypes
trainableOnlyoption 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 |