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

From Leeroopedia


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

Overview

This is one of the largest test suites (3198 lines), providing exhaustive validation of all recurrent layer implementations in TensorFlow.js Layers. It covers the low-level rnn() function, the RNN base layer, SimpleRNN, GRU, and LSTM layers (including their cell variants), and StackedRNNCells for multi-layer recurrent networks. Each RNN type is tested at symbolic (shape inference), tensor (numerical correctness against Python Keras reference), and serialization levels. Tests cover return sequences, return state, go backwards, masking, dropout, recurrent dropout, stateful mode, bidirectional compatibility, and various activation/initializer/regularizer/constraint configurations.

Code Reference

Source Location: tfjs-layers/src/layers/recurrent_test.ts (3198 lines)

Repository: GitHub

Test Describe Blocks

  • rnn - Low-level RNN step function (3D inputs, 1 state, 2 states, goBackwards, masking)
  • RNN-Layer - RNN base layer symbolic tests (cell wrapping, stateSpec)
  • RNN-Layer-Math - RNN base layer tensor computation
  • SimpleRNN Symbolic - SimpleRNN shape inference (returnSequences, returnState)
  • SimpleRNN Tensor - SimpleRNN numerical correctness against reference values
  • SimpleRNN Serialization - Config getConfig/fromConfig round-trips
  • GRU Symbolic - GRU shape inference
  • GRU Tensor - GRU numerical correctness (hardSigmoid, resetAfter, goBackwards, masking, dropout)
  • GRU-deserialization - GRU model JSON deserialization
  • GRU Serialization - GRU config round-trips with initializers, regularizers, constraints
  • LSTM Symbolic - LSTM shape inference (returnSequences, returnState)
  • LSTM Tensor - LSTM numerical correctness (hardSigmoid, goBackwards, masking, stateful, dropout, initialState)
  • LSTM-deserialization - LSTM model JSON deserialization from Python Keras
  • LSTM Serialization - LSTM config round-trips
  • StackedRNNCells Symbolic - Multi-layer RNN shape inference
  • Stacked RNN serialization - Stacked cell config round-trips
  • StackedRNNCells Tensor - Multi-layer RNN numerical computation

I/O Contract

Inputs to tests:

  • 3D input tensors: [batch, timesteps, features]
  • Initial state tensors for each recurrent cell
  • RNN configuration: units, activation, returnSequences, returnState, goBackwards, unroll, stateful
  • Dropout and recurrent dropout rates
  • Masks for variable-length sequence support

Expected outputs/assertions:

  • Last output, per-step outputs, and new state tensors have correct shapes
  • Numerical values match Python Keras reference values (verified to multiple decimal places)
  • goBackwards reverses sequence processing order
  • Masking correctly ignores masked timesteps
  • Stateful RNNs maintain state across batches
  • Serialization preserves all configuration including nested cell configs

Usage Example

describeMathCPUAndGPU('rnn', () => {
  it('Simple step function: 3D inputs, 1 state', () => {
    const inputs = tensor3d(
        [[[1, 2], [3, 4], [5, 6]], [[10, 20], [30, 40], [50, 60]]],
        [2, 3, 2]);
    const initialStates = [tfc.zeros([2, 4])];
    const rnnOutputs = rnn(
        rnnStepForTest, inputs, initialStates,
        false, null, null, false, true);
    const lastOutput = rnnOutputs[0];
    expectTensorsClose(lastOutput, tensor2d(
        [[-57.75, -57.75, -57.75, -57.75],
         [-57.75, -57.75, -57.75, -57.75]], [2, 4]));
  });
});

Test Coverage Summary

Category Count Details
rnn() function 10+ Step functions, states, backwards, masking
SimpleRNN 30+ Symbolic, tensor, serialization
GRU 40+ Symbolic, tensor, deserialization, serialization, resetAfter
LSTM 50+ Symbolic, tensor, deserialization, serialization, stateful
StackedRNNCells 10+ Multi-layer symbolic, tensor, serialization
Test Environment Mixed CPU, GPU, WebGL2

Related Pages

Page Connections

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