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

From Leeroopedia


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

Overview

This test suite validates the LayerVariable class and related variable management functions in TensorFlow.js Layers. LayerVariables wrap TensorFlow.js tensors with additional metadata (name, dtype, trainability, shape) and are used to represent layer weights. The tests cover variable construction (with and without explicit names, name scopes, dtypes), reading and writing values, variable creation helpers (zerosVariable, onesVariable, zerosLike, onesLike, eye), variable updates, batch get/set operations, and gradient computation through variables.

Code Reference

Source Location: tfjs-layers/src/variables_test.ts (457 lines)

Repository: GitHub

Test Describe Blocks

  • Variable - Core LayerVariable tests:
    • Constructor: no explicit name (auto-naming), explicit name, name with name scope
    • Trainable property
    • Null/undefined name handling
    • Int32 and bool dtype support
    • Read value, write value, dispose
    • Constraint setting and retrieval
    • Unique name generation across instances
  • Create Variable - Variable creation from concrete tensors
  • ZerosVariable - Creating variables initialized to zeros (various shapes, dtypes)
  • OnesVariable - Creating variables initialized to ones
  • ZerosLike - Creating zero-valued variables matching shape of another variable
  • OnesLike - Creating one-valued variables matching shape of another variable
  • eye (I-matrix builder) - Creating identity matrix variables
  • Variable update - Updating variable values with new tensors (element-wise operations)
  • batchGetValue - Batch retrieval of values from multiple variables
  • batchSetValue - Batch setting of values for multiple variables
  • gradients - Gradient computation through variable operations

I/O Contract

Inputs to tests:

  • Tensor values: zeros, ones, scalars, random uniform
  • Shape arrays for variable creation
  • Name strings and name scopes
  • Dtype specifications: float32, int32, bool
  • Gradient target expressions using variable operations

Expected outputs/assertions:

  • Variable names include class prefix or explicit name, with name scope path
  • Auto-generated names are unique across instances
  • Read returns correct tensor values
  • Write updates the internal tensor
  • Dtype is preserved correctly
  • zerosVariable/onesVariable create variables with correct values and dtypes
  • zerosLike/onesLike match the shape and dtype of the source
  • eye creates correct identity matrices
  • Gradient computation returns correct derivative values

Usage Example

describeMathCPU('Variable', () => {
  it('Variable constructor: no explicit name', () => {
    const v1 = new V.LayerVariable(zeros([2]));
    expect(v1.name.indexOf('Variable')).toEqual(0);
    expect(v1.dtype).toEqual('float32');
    expect(v1.shape).toEqual([2]);
    expect(v1.trainable).toEqual(true);
    expect(v1.read().dataSync()).toEqual(new Float32Array([0, 0]));
  });

  it('Variable constructor: explicit name with name scope', () => {
    let v1: V.LayerVariable;
    nameScope('barScope', () => {
      nameScope('bazScope', () => {
        v1 = new V.LayerVariable(scalar(0), undefined, 'foo');
      });
    });
    expect(v1.name.indexOf('barScope/bazScope/foo')).toEqual(0);
  });
});

Test Coverage Summary

Category Count Details
Constructor 8+ Auto-naming, explicit name, name scope, dtype, trainable
Read/Write 3+ Value access and mutation
Creation Helpers 15+ zeros, ones, zerosLike, onesLike, eye
Variable Update 3+ Element-wise update operations
Batch Operations 4+ batchGetValue, batchSetValue
Gradients 3+ Gradient computation through variables
Test Environment Mixed CPU, GPU

Related Pages

Page Connections

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