Implementation:Tensorflow Tfjs Layer Utils
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Layers_API, Utilities |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
This module provides utility functions for printing human-readable summaries of TensorFlow.js LayersModel instances. It formats layer names, types, input shapes, output shapes, parameter counts, and connectivity information in a tabular text layout. The main entry point is printSummary, which adapts its column layout based on whether the model has a sequential-like topology or a more complex graph structure.
Code Reference
Source Location
tfjs-layers/src/utils/layer_utils.ts (GitHub)
Key Imports
import {Container} from '../engine/container';
import {Layer, Node} from '../engine/topology';
import {countParamsInWeights} from './variable_utils';
Main Function
printSummary
export function printSummary(
model: Container,
lineLength?: number,
positions?: number[],
printFn: (message?: any, ...optionalParams: any[]) => void = console.log
): void
Prints a formatted summary of the model to the console (or a custom print function). Columns include:
- Layer (type) - Layer name and class name
- Input Shape - Shapes of inbound tensors
- Output shape - The output tensor shape
- Param # - Number of trainable parameters
- Receives inputs (non-sequential models only) - Connectivity info
Layout Defaults
| Model Type | Line Length | Column Positions |
|---|---|---|
| Sequential-like | 90 | [0.32, 0.61, 0.89, 1] |
| Non-sequential (graph) | 115 | [0.24, 0.48, 0.70, 0.80, 1] |
Internal Functions
countTrainableParams(model): Counts total trainable parameters, usingcollectedTrainableWeightsif available.isModelSequentialLike(model): Determines if the model has a linear topology (single path, no shared layers).printRow(fields, positions, printFn): Formats a row of fields at specified column positions.printLayerSummary(layer, positions, printFn): Prints one layer without connectivity info (sequential models).printLayerSummaryWithConnections(layer, positions, relevantNodes, printFn): Prints one layer with inbound connection details (graph models).
I/O Contract
| Function | Input | Output |
|---|---|---|
printSummary |
Container (model), optional line length, positions, print function |
void (text output via printFn) |
The summary footer includes total params, trainable params, and non-trainable params counts.
Usage Example
import * as tf from '@tensorflow/tfjs';
const model = tf.sequential();
model.add(tf.layers.dense({units: 64, inputShape: [128]}));
model.add(tf.layers.dense({units: 10}));
model.summary(); // internally calls printSummary
// Custom print function to capture output
const lines: string[] = [];
model.summary(undefined, undefined, (line: string) => lines.push(line));
Related Pages
- Tensorflow_Tfjs_Training_Utils - Training weight utilities
- Tensorflow_Tfjs_Embedding_Layer - Example layer whose params appear in summaries
- Tensorflow_Tfjs_DepthwiseConv2D_Layer - Convolutional layer with parameter counts