Implementation:Tensorflow Tfjs Container Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Layers_API |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
This test suite validates the Container class, which is the foundation of the Functional API model in TensorFlow.js Layers. Containers manage the computational graph of layers, tracking inputs, outputs, and the topology of connected layers. The tests cover container construction from configuration (including deserialization from Python Keras configs), layer connectivity, loss calculation, source input resolution, and model disposal/memory management.
Code Reference
Source Location: tfjs-layers/src/engine/container_test.ts (695 lines)
Repository: GitHub
Test Describe Blocks
Container.fromConfig- Creating containers from serialized config (minimal, simple network, multi-node, multi-output)Container- Layer connectivity, weight management, naming, output shape, stateful properties, call hooks, nested modelsContainer.calculateLosses- Kernel and bias regularization loss computationgetSourceInputs()- Tracing back through the graph to find source input tensorsLayersModel-dispose- Memory management and tensor disposal when freeing models
I/O Contract
Inputs to tests:
- Serialized model configurations matching Python Keras output (JSON with layer configs, inbound nodes, input/output layers)
- Layer instances connected via SymbolicTensors
- Regularizer configurations (L1L2)
Expected outputs/assertions:
- Containers reconstruct correct topology from config (layer names, connections, shapes)
- Output shapes are correctly computed from the graph
- Weight counts match expected values
- Regularization losses are computed correctly
- Model disposal frees all weight tensors and reduces memory tensor count
Usage Example
describeMathCPUAndGPU('Container.fromConfig', () => {
it('creates a minimal Container from simplest config', () => {
const config = {
name: 'test',
layers: [] as any[],
inputLayers: [] as any[],
outputLayers: [] as any[]
};
const container =
Container.fromConfig(ContainerForTest, config) as Container;
expect(container.name).toEqual('test');
});
});
Test Coverage Summary
| Category | Count | Details |
|---|---|---|
| fromConfig | 3+ | Minimal, simple network, multi-node topologies |
| Container Properties | 10+ | Weights, naming, output shape, stateful, call hooks |
| Loss Calculation | 3+ | Kernel regularization, bias regularization, layer losses |
| Source Inputs | 2+ | Tracing through graph to source inputs |
| Disposal | 3+ | Memory cleanup, tensor count verification |
| Test Environment | CPU and GPU | describeMathCPUAndGPU
|