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

From Leeroopedia
Revision as of 16:52, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tensorflow_Tfjs_Wrappers_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 wrapper layers in TensorFlow.js Layers: TimeDistributed and Bidirectional. TimeDistributed applies a layer independently to each timestep of a sequence, enabling the use of non-recurrent layers (e.g., Dense) on sequential data. Bidirectional runs a recurrent layer in both forward and backward directions and merges the results using configurable merge modes (concat, sum, mul, ave). Tests cover symbolic shape inference, tensor computation, serialization, initial state handling, and masking support.

Code Reference

Source Location: tfjs-layers/src/layers/wrappers_test.ts (785 lines)

Repository: GitHub

Test Describe Blocks

  • TimeDistributed Layer: Symbolic - Shape inference for Dense and Reshape wrapped in TimeDistributed (3D, 4D input; 2D exception)
  • TimeDistributed Layer: Tensor - Numerical correctness with Dense, model-as-layer, and serialization
  • Bidirectional Layer: Symbolic - Shape inference for all merge modes (concat, sum, mul, ave), returnState, returnSequences, SimpleRNN/LSTM/GRU wrapped layers
  • checkBidirectionalMergeMode - Validation of merge mode strings
  • Bidirectional Layer: Tensor - Numerical correctness with various merge modes, concat with returnSequences, goBackwards, serialization round-trips
  • Bidirectional with initial state - Forward and backward initial states for LSTM and SimpleRNN
  • Bidirectional with masking - Masking support with Embedding layer and Bidirectional LSTM

I/O Contract

Inputs to tests:

  • 3D input tensors for TimeDistributed: [batch, timesteps, features]
  • 4D input tensors for TimeDistributed with Reshape
  • Wrapped layer configurations (Dense, SimpleRNN, LSTM, GRU)
  • Merge mode selections: 'concat', 'sum', 'mul', 'ave'
  • Initial state tensors for bidirectional layers
  • Masking values for variable-length sequences

Expected outputs/assertions:

  • TimeDistributed output shape: [batch, timesteps, wrappedOutputDim]
  • Bidirectional with concat: output features doubled
  • Bidirectional with sum/mul/ave: output features same as wrapped layer
  • Numerical values match expected computations (e.g., all-ones kernel)
  • Serialization preserves wrapped layer config and merge mode
  • Initial states correctly applied to forward and backward passes

Usage Example

describeMathCPU('TimeDistributed Layer: Symbolic', () => {
  it('3D input: Dense', () => {
    const input = new tfl.SymbolicTensor('float32', [10, 8, 2], null, [], null);
    const wrapper = tfl.layers.timeDistributed({
      layer: new Dense({units: 3})
    });
    const output = wrapper.apply(input) as tfl.SymbolicTensor;
    expect(wrapper.trainable).toEqual(true);
    expect(wrapper.getWeights().length).toEqual(2);
    expect(output.shape).toEqual([10, 8, 3]);
  });
});

Test Coverage Summary

Category Count Details
TimeDistributed Symbolic 4+ Dense, Reshape, error cases, serialization
TimeDistributed Tensor 5+ Dense, model-as-layer, config round-trips
Bidirectional Symbolic 10+ All merge modes, RNN types, returnState
Bidirectional Tensor 10+ Merge modes, goBackwards, serialization
Initial State 5+ LSTM and SimpleRNN with initial states
Masking 3+ Embedding + Bidirectional LSTM masking
Test Environment Mixed CPU, WebGL2

Related Pages

Page Connections

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