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 Backend Operations Test

From Leeroopedia
Revision as of 16:50, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tensorflow_Tfjs_Backend_Operations_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 TensorFlow.js core backend operations used by the Layers API. It covers fundamental tensor manipulation operations including type casting, reshaping, slicing, concatenation, mathematical operations (dot product, sign, square, pow), dropout, bias addition, and activation functions at the backend level. The tests verify correct behavior for backend configuration, dtype handling, parameter counting, and tensor memory management.

Code Reference

Source Location: tfjs-layers/src/backend/tfjs_backend_test.ts (895 lines)

Repository: GitHub

Test Describe Blocks

  • TensorMath - Backend setting and getting
  • dtype - Data type resolution for Tensor and SymbolicTensor
  • countParams - Parameter counting across tensor dimensions
  • cast - Type casting between float32, int32, and bool
  • expandDims - Dimension expansion operations
  • Repeat - Tensor repetition along axes
  • Flatten - Tensor flattening operations
  • batchFlatten - Batch-aware flattening
  • sliceAlongFirstAxis - Slicing along first dimension
  • sliceAlongLastAxis - Slicing along last dimension
  • sliceAlongAxis - General axis slicing
  • concatenate - Tensor concatenation along axes
  • concatAlongFirstAxis - Concatenation along first axis
  • tile - Tensor tiling operations
  • randomNormal - Random normal tensor generation
  • dot - Dot product operations
  • sign - Element-wise sign function
  • OneHot - One-hot encoding
  • Gather - Tensor gathering by indices
  • Square - Element-wise squaring
  • Pow - Element-wise power operations
  • softsign - Softsign activation at backend level
  • dropout - Dropout implementation with rate and noise shape
  • biasAdd - Bias addition with data format support
  • elu - ELU activation at backend level
  • Sigmoid - Sigmoid at backend level
  • hardSigmoid - Hard sigmoid at backend level
  • inTrainPhase - Training phase branching

I/O Contract

Inputs to tests:

  • Tensors of various ranks (scalar through 6D) with known numeric values
  • Backend identifiers (e.g., 'cpu', 'webgl')
  • Data types (float32, int32, bool)
  • Configuration parameters for dropout rates, noise shapes, data formats

Expected outputs/assertions:

  • Correct output dtype and shape after operations
  • Numerically accurate results matching hand-computed expected values
  • Proper parameter counts for tensors of different ranks
  • Memory safety verified through expectNoLeakedTensors

Usage Example

describeMathCPU('countParams', () => {
  it('Tensor2D', () => {
    const x = zeros([3, 2]);
    expect(K.countParams(x)).toEqual(6);
    expect(K.countParams(new LayerVariable(x).read())).toEqual(6);
  });
});

describeMathCPUAndGPU('cast', () => {
  it('float32 to int32', () => {
    const x = tensor2d([[-1.1, -1.6], [1.1, 2.2], [3.6, 4.7]], [3, 2], 'float32');
    const y = K.cast(x, 'int32');
    expect(y.dtype).toEqual('int32');
    expect(Array.from(y.dataSync())).toEqual([-1, -1, 1, 2, 3, 4]);
  });
});

Test Coverage Summary

Category Count Details
Backend Configuration 2 TensorMath, dtype
Tensor Manipulation 10 expand, repeat, flatten, slice, concat, tile, gather
Math Operations 6 dot, sign, square, pow, oneHot, randomNormal
Activations 5 softsign, elu, sigmoid, hardSigmoid, dropout
Utility 2 biasAdd, inTrainPhase
Test Environment CPU and GPU Mix of describeMathCPU and describeMathCPUAndGPU

Related Pages

Page Connections

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