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

From Leeroopedia


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

Overview

This test suite validates the ConvLSTM2D layer and ConvLSTM2DCell, which combine convolutional operations with LSTM recurrence for spatiotemporal sequence processing (e.g., video prediction, weather forecasting). The tests cover cell-level output correctness across data formats (channelsFirst/channelsLast), filter counts, kernel sizes, and padding modes using Cartesian product enumeration. It also tests symbolic shape inference, tensor-level computation, backpropagation through time (BPTT), and serialization/deserialization round-trips including JSON model loading.

Code Reference

Source Location: tfjs-layers/src/layers/convolutional_recurrent_test.ts (891 lines)

Repository: GitHub

Test Describe Blocks

  • ConvLSTM2DCell - Cell-level correctness tests with Cartesian products of:
    • dataFormat: channelsFirst, channelsLast
    • filters: 3, 5, 9
    • kernelSize: 3, 5
    • padding: valid, same
  • ConvLSTM2D Symbolic - Symbolic shape inference for various configurations (returnSequences, returnState, goBackwards)
  • ConvLSTM2D Tensor - Concrete tensor computation with real data, verifying output values against Python Keras reference
  • should run BPTT correctly - Backpropagation through time verification
  • ConvLSTM2D Serialization and Deserialization - Config getConfig/fromConfig round-trips, JSON model loading with initializers, constraints, regularizers, and activations

I/O Contract

Inputs to tests:

  • 5D input tensors: [batch, timesteps, channels, height, width] or [batch, timesteps, height, width, channels]
  • Cell configuration: dataFormat, filters, kernelSize, padding, initializers (ones, glorotUniform, heUniform)
  • Serialized model configurations for deserialization tests

Expected outputs/assertions:

  • Cell outputs and states (h, c) have correct shapes based on filters, kernelSize, and padding
  • Mean values of outputs match Python Keras reference values
  • Symbolic output shapes correctly inferred for all configurations
  • BPTT produces valid gradients
  • Serialization preserves all configuration including initializers, constraints, regularizers

Usage Example

describeMathCPUAndGPU('ConvLSTM2DCell', () => {
  describe('should return the correct outputs', () => {
    const testArgs = getCartesianProductOfValues(
        dataFormatValues, filterValues, kernelSizeValues, paddingValues);

    for (const [dataFormat, filters, kernelSize, padding] of testArgs) {
      it(`for dataFormat=${dataFormat}, filters=${filters}`, () => {
        const cell = tfl.layers.convLstm2dCell({
          dataFormat, filters, kernelSize, padding,
          kernelInitializer: 'ones',
          recurrentInitializer: 'ones',
          biasInitializer: 'ones',
        });
        // ... verify output shapes and values
      });
    }
  });
});

Test Coverage Summary

Category Count Details
Cell Output Tests 24+ Cartesian product of dataFormat x filters x kernelSize x padding
Symbolic Tests 10+ Shape inference with returnSequences, returnState
Tensor Tests 5+ Concrete computation against reference values
BPTT 2+ Gradient computation through time
Serialization 5+ Config round-trips, JSON model loading
Test Environment Mixed CPU, GPU, WebGL2

Related Pages

Page Connections

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