Implementation:Tensorflow Tfjs Model Summary Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Layers_API |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
This test suite validates the LayersModel.summary() and Sequential.summary() methods, which print a human-readable table of model architecture information to the console. The tests verify the exact formatting of summary output including layer names, types, input shapes, output shapes, parameter counts, separator lines, and totals. It also tests customization options: custom line length, custom column positions, and custom print functions that redirect output away from console.log.
Code Reference
Source Location: tfjs-layers/src/model_summary_test.ts (337 lines)
Repository: GitHub
Test Describe Blocks
LayersModel.summary- All summary tests including:- Sequential model: one layer (default formatting)
- Sequential model: one layer with custom lineLength
- Sequential model: one layer with custom positions
- Sequential model: one layer with custom printFn
- Sequential model: multiple layers
- Functional model summary
- Multi-input model summary
- Multi-output model summary
- Nested model summary
- Non-trainable params display
I/O Contract
Inputs to tests:
- Sequential models with one or more Dense layers
- Functional models with single and multiple inputs/outputs
- Summary configuration: lineLength (number), positions (number[]), printFn (custom function)
Expected outputs/assertions:
- Console output lines match exact expected strings including:
- Header:
'Layer (type) Input Shape Output shape Param # ' - Layer rows with correctly formatted name, type, shapes, and param count
- Separator lines using
=and_characters - Footer with total params, trainable params, and non-trainable params
- Header:
- Custom lineLength adjusts column widths
- Custom positions array adjusts column boundaries
- Custom printFn receives the same output that console.log would
Usage Example
describeMathCPU('LayersModel.summary', () => {
let consoleLogHistory: string[];
beforeEach(() => {
consoleLogHistory = [];
spyOn(console, 'log').and.callFake((message?: string) => {
consoleLogHistory.push(message);
});
});
it('Sequential model: one layer', () => {
const layerName = getRandomLayerOrModelName();
const model = tfl.sequential({
layers: [tfl.layers.dense({units: 3, inputShape: [10], name: layerName})]
});
model.summary();
expect(consoleLogHistory).toEqual([
'__________________________...',
'Layer (type) Input Shape...',
'==========================...',
`${layerName} (Dense) [[null,10]]...`,
'==========================...',
'Total params: 33', 'Trainable params: 33',
'Non-trainable params: 0', '__________________________...'
]);
});
});
Test Coverage Summary
| Category | Count | Details |
|---|---|---|
| Default Summary | 2+ | Single and multi-layer Sequential models |
| Custom Formatting | 3 | lineLength, positions, printFn |
| Model Types | 4+ | Sequential, Functional, multi-input, multi-output |
| Nested Models | 1+ | Models containing sub-models |
| Non-trainable Params | 1+ | Correct param count split |
| Test Environment | CPU | describeMathCPU
|