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 Training 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 (2991 lines), providing comprehensive validation of the core training engine in TensorFlow.js Layers. It tests the LayersModel.fit(), LayersModel.predict(), LayersModel.evaluate(), LayersModel.trainOnBatch(), and LayersModel.execute() methods. The suite also validates utility functions for data standardization (isDataTensor, isDataArray, isDataDict, standardizeInputData), array length checking, metric collection, batch slicing, and memory leak prevention during training loops.

Code Reference

Source Location: tfjs-layers/src/engine/training_test.ts (2991 lines)

Repository: GitHub

Test Describe Blocks

  • isDataTensor - Type guard for single tensor inputs
  • isDataArray - Type guard for tensor array inputs
  • isDataDict - Type guard for named tensor dict inputs
  • standardizeInputData - Normalizing input data to standard array format
  • checkArrayLengths - Validating matching lengths of input/output arrays
  • collectMetrics - Gathering metrics for single and multi-output models
  • sliceArraysByIndices - Extracting tensor slices by index arrays
  • makeBatches - Creating batch boundaries from dataset size
  • LayersModel.predict - Model inference with various configurations
  • LayersModel.fit long - Extended training tests
  • LayersModel.fit - Core training loop (largest block: compilation, metrics, callbacks, validation split, class weights, sample weights, regularizers, multi-output)
  • LayersModel.fit: No memory leak - Memory leak prevention during training
  • LayersModel.fit: yieldEvery - Yield control during training
  • LayersModel.trainOnBatch - Single-batch training
  • LayersModel.evaluate - Model evaluation
  • LayersModel trainable setter and getter - Trainability toggling
  • LayersModel.execute - Direct execution of model subgraphs

I/O Contract

Inputs to tests:

  • Tensor inputs in three forms: single tensor, array of tensors, or named tensor dict
  • Model architectures: sequential and functional with Dense, Reshape layers
  • Training config: loss functions, optimizers (SGD, Adam), metrics, epochs, batch size, validation split
  • Regularizers (L1, L2, L1L2) on kernel and bias

Expected outputs/assertions:

  • Data type guards correctly identify tensor, array, and dict formats
  • standardizeInputData normalizes all input forms to arrays, throwing on mismatches
  • Training reduces loss over epochs
  • Memory leak checks: tensor count stable before/after fit()
  • Metric values match reference Python Keras outputs
  • trainOnBatch returns correct loss/metric values
  • Evaluate returns correct metrics for test data

Usage Example

describeMathCPU('isDataTensor', () => {
  const x = tfc.tensor2d([[3.14]]);
  it('Positive case', () => {
    expect(isDataTensor(x)).toEqual(true);
  });
  it('Negative cases', () => {
    expect(isDataTensor([x, x])).toEqual(false);
    expect(isDataTensor({'Pie': x})).toEqual(false);
  });
});

describeMathCPU('standardizeInputData', () => {
  it('Singleton Tensor, Array of one name', () => {
    const outputs = standardizeInputData(getX(), ['Foo']);
    expect(outputs.length).toEqual(1);
    expectTensorsClose(outputs[0], getX());
  });
});

Test Coverage Summary

Category Count Details
Data Utilities 20+ Type guards, standardization, batching
Model.predict 10+ Single/multi output, batch predictions
Model.fit 50+ Loss, metrics, callbacks, validation, regularizers
Memory Leaks 15+ Tensor count verification across training
Model.trainOnBatch 5+ Single batch training
Model.evaluate 5+ Model evaluation metrics
Model.execute 5+ Subgraph execution
Test Environment Mixed CPU, GPU, WebGL2 variants

Related Pages

Page Connections

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