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 Base Callbacks Test

From Leeroopedia


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

Overview

This test suite validates the callback infrastructure used during model training in TensorFlow.js Layers. It covers the BaseLogger callback (loss averaging), CallbackList management, History callback for epoch tracking, callback lifecycle hooks (onTrainBegin, onEpochEnd, onBatchEnd), log resolution (scalar tensors to numbers), tensor disposal in logs, and the CallbackConstructorRegistry for registering custom callback constructors. These callbacks form the foundation of training event handling.

Code Reference

Source Location: tfjs-layers/src/base_callbacks_test.ts (379 lines)

Repository: GitHub

Test Describe Blocks

  • BaseLogger Callback - Records and averages losses within an epoch, forgets old epochs
  • CallbackList - Constructor with empty/array args, parameter setting, lifecycle hooks
  • resolveScalarsInLogs - Converts scalar tensors to JavaScript numbers in log objects
  • disposeTensorsInLogs - Disposes tensor values in log objects to prevent memory leaks
  • History Callback - Tracks training history across epochs
  • CallbackList (second block) - Additional callback list tests
  • CallbackConstructorRegistry - Registry for custom callback class constructors
  • CallbackConstructorRegistry initialization - Math-environment initialization tests
  • LayersModel.fit and CallbackConstructorRegistry - Integration with model training

I/O Contract

Inputs to tests:

  • Simulated batch logs with loss values and batch sizes (e.g., {batch: 0, size: 10, loss: 5})
  • Callback arrays and parameter dictionaries
  • Scalar tensors and unresolved log objects

Expected outputs/assertions:

  • BaseLogger correctly computes weighted average loss: (10 * 5 + 10 * 6 + 5 * 7) / (10 + 10 + 5)
  • History callback records epoch indices and metric values
  • CallbackList propagates lifecycle events to all registered callbacks
  • Scalar tensors in logs are resolved to numeric values
  • Tensors in logs are properly disposed

Usage Example

describe('BaseLogger Callback', () => {
  it('Records and averages losses in an epoch', async () => {
    const baseLogger = new BaseLogger();
    baseLogger.setParams({metrics: ['loss', 'val_loss']});
    await baseLogger.onTrainBegin();
    await baseLogger.onEpochBegin(0);
    await baseLogger.onBatchEnd(0, {batch: 0, size: 10, loss: 5});
    await baseLogger.onBatchEnd(1, {batch: 1, size: 10, loss: 6});
    await baseLogger.onBatchEnd(2, {batch: 2, size: 5, loss: 7});
    const epochLog: UnresolvedLogs = {val_loss: 3};
    await baseLogger.onEpochEnd(0, epochLog);
    expect(epochLog['loss'] as number)
        .toBeCloseTo((10 * 5 + 10 * 6 + 5 * 7) / (10 + 10 + 5));
  });
});

Test Coverage Summary

Category Count Details
BaseLogger 2 Loss averaging, epoch forgetting
CallbackList 5+ Construction, params, lifecycle hooks
Log Resolution 2 resolveScalarsInLogs, disposeTensorsInLogs
History 2+ Epoch tracking, metric recording
Registry 4+ Registration, clearing, integration with model.fit
Test Environment Mixed describe, describeMathCPUAndGPU

Related Pages

Page Connections

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