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 Core Layers Test

From Leeroopedia


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

Overview

This test suite validates the core layer types in TensorFlow.js Layers: Dropout, SpatialDropout1D, Dense, Flatten, Activation, RepeatVector, Reshape, Permute, and Masking. These are the fundamental building blocks of most neural networks. Each layer is tested at both symbolic (shape inference, configuration) and tensor (numerical correctness) levels. The suite covers dropout behavior during training vs inference, Dense layer with various activations and initializers, Flatten for reshaping multi-dimensional tensors, and utility layers like Reshape and Permute.

Code Reference

Source Location: tfjs-layers/src/layers/core_test.ts (871 lines)

Repository: GitHub

Test Describe Blocks

  • Dropout Layer: Symbolic - Shape inference with various dropout rates and input shapes
  • Dropout Layer - Tensor-level dropout behavior (training vs inference, noise shapes, scaling)
  • SpatialDropout1D Layer - Spatial dropout for 1D feature maps
  • Dense Layer: Symbolic - Shape inference, activations, regularizers, constraints, initializers
  • Dense Layer: Tensor - Numerical output verification with known kernels and biases
  • Flatten Layer: Symbolic - Shape inference for various input shapes and data formats
  • Flatten Layer: Tensor - Tensor reshaping correctness
  • Activation Layer: Tensor - Standalone activation layer with various activation functions
  • RepeatVector Layer: Symbolic - Shape inference for vector repetition
  • RepeatVector Layer: Tensor - Numerical repetition correctness
  • Reshape Layer: Symbolic - Shape inference for reshape operations
  • Reshape Layer: Tensor - Tensor reshaping correctness
  • Permute Layer: Symbolic - Shape inference for dimension permutation
  • Masking Layer: Symbolic - Shape inference for masking layers
  • Permute Layer: Tensor - Dimension permutation correctness
  • Masking Layer: Tensor - Masking behavior with custom mask values

I/O Contract

Inputs to tests:

  • SymbolicTensors with known shapes for symbolic tests
  • Concrete tensors (ones, zeros, random) for tensor tests
  • Layer configurations: units, activation, dropout rate, noise shape, target shape, dims
  • Training flag to control dropout behavior

Expected outputs/assertions:

  • Dropout outputs equal inputs during inference (training=false)
  • During training, dropout zeros out elements and scales remaining by 1/(1-rate)
  • Dense layer produces correct matrix multiplication results
  • Flatten correctly reshapes to 2D preserving batch dimension
  • Reshape and Permute produce correct output shapes and values
  • Masking correctly masks timesteps matching the mask value

Usage Example

describe('Dropout Layer: Symbolic', () => {
  for (const rate of [0, 0.5]) {
    for (const symbolicInput of symbolicInputs) {
      it(`dropoutRate=${rate}; input shape=${JSON.stringify(symbolicInput.shape)}`, () => {
        const dropoutLayer = tfl.layers.dropout({rate});
        const output = dropoutLayer.apply(symbolicInput) as tfl.SymbolicTensor;
        expect(output.dtype).toEqual(symbolicInput.dtype);
        expect(output.shape).toEqual(symbolicInput.shape);
      });
    }
  }
});

Test Coverage Summary

Category Count Details
Dropout 15+ Symbolic and tensor tests, noise shapes, training/inference
Dense 20+ Activations, initializers, regularizers, constraints, serialization
Flatten 10+ Various input shapes, data formats
Activation 5+ Standalone activation layer
Reshape/Permute 10+ Shape transformation layers
Masking 5+ Mask value and output masking
Test Environment Mixed CPU, GPU, WebGL2

Related Pages

Page Connections

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