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 Topology Test

From Leeroopedia
Revision as of 16:52, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tensorflow_Tfjs_Topology_Test.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

Overview

This test suite validates the core topology primitives of the TensorFlow.js Layers engine: InputSpec, Node, SymbolicTensor, and Layer. These are the building blocks that define how layers connect and data flows through a neural network graph. The tests cover input specification validation, node creation and serialization, layer construction (naming, weight management, trainability, state), layer apply behavior with both symbolic and concrete tensors, input compatibility checking, and weight loading from named tensor maps.

Code Reference

Source Location: tfjs-layers/src/engine/topology_test.ts (1181 lines)

Repository: GitHub

Test Describe Blocks

  • InputSpec - Default values, ndim from shape, axes specification
  • Node - Object initialization, serializable node config generation
  • Layer - Comprehensive layer tests including:
    • Constructor (naming, auto-incrementing, dtype, initial weight)
    • apply() with SymbolicTensors and concrete tensors
    • Weight management (trainable/non-trainable, setting weights, counting params)
    • Input compatibility checking (ndim, dtype, axes constraints)
    • Stateful layers (resetStates)
    • Masking support (supportsMasking, computeMask)
    • Config serialization (getConfig)
  • Layer-dispose - Disposing layers and verifying tensor cleanup
  • loadWeightsFromNamedTensorMap - Loading weights from key-value tensor maps with strict/non-strict modes

I/O Contract

Inputs to tests:

  • Layer configurations (name, dtype, inputShape, batchInputShape, trainable)
  • SymbolicTensors with known shapes and dtypes
  • Concrete tensors (zeros, ones, random) for apply() calls
  • Named tensor maps for weight loading

Expected outputs/assertions:

  • InputSpec properties match constructor args
  • Nodes correctly track inbound/outbound layers and tensor indices
  • Layer names auto-increment and respect name scopes
  • apply() produces correct output shapes for both symbolic and concrete tensors
  • Input compatibility errors thrown for mismatched ndim, dtype, or axes
  • Weight loading correctly matches tensors by name, with strict mode errors for missing weights

Usage Example

describe('InputSpec', () => {
  it('initializes with expected default values.', () => {
    const inputSpec = new InputSpec({});
    expect(inputSpec.dtype).toBeUndefined();
    expect(inputSpec.shape).toBeUndefined();
    expect(inputSpec.ndim).toBeUndefined();
    expect(inputSpec.maxNDim).toBeUndefined();
    expect(inputSpec.minNDim).toBeUndefined();
    expect(inputSpec.axes).toEqual({});
  });
});

describe('Node', () => {
  it('initializes object as expected.', () => {
    const outboundLayer = new LayerForTest({name: 'outboundLayer'});
    const inboundLayers = [new LayerForTest({name: 'inboundLayer'})];
    const node = new Node({
      outboundLayer, inboundLayers,
      nodeIndices: [0], tensorIndices: [0],
      inputTensors: [new tfl.SymbolicTensor('float32', [1], null, [], {})],
      outputTensors: [new tfl.SymbolicTensor('float32', [2, 2], null, [], {})],
      inputMasks: [zeros([1])], outputMasks: [zeros([1])],
      inputShapes: [[1]], outputShapes: [[1], [1]]
    }, {});
    expect(node.outboundLayer).toEqual(outboundLayer);
  });
});

Test Coverage Summary

Category Count Details
InputSpec 3 Default values, ndim inference, axes
Node 2+ Initialization, serialization
Layer Construction 15+ Naming, dtype, weights, trainability
Layer.apply() 10+ Symbolic and concrete tensor modes
Input Compatibility 5+ ndim, dtype, axes validation
Layer Disposal 5+ Tensor cleanup, shared weight handling
Weight Loading 5+ Named tensor map, strict mode
Test Environment Mixed describeMathCPU, describeMathCPUAndGPU

Related Pages

Page Connections

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