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

From Leeroopedia


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

Overview

The tfjs_backend.ts module provides the backend utility functions that form the computational foundation of the TensorFlow.js Layers API. These functions wrap and extend @tensorflow/tfjs-core operations for use by higher-level layer implementations. The module covers tensor manipulation (slicing, concatenation, tiling, reshaping), neural network operations (bias addition, dropout, activation functions), linear algebra (dot product with fused matmul support), and random tensor generation.

The default backend is WebGL (GPU), configurable via setBackend(). All functions operate eagerly (not symbolically).

Code Reference

Source Location

tfjs-layers/src/backend/tfjs_backend.ts (View on GitHub)

Key Signatures

// Tensor information
export function countParams(x: HasShape): number;

// Tensor manipulation
export function cast(x: Tensor, dtype: tfc.DataType): Tensor;
export function expandDims(x: Tensor, axis?: number): Tensor;
export function repeat(x: Tensor, n: number): Tensor;
export function flatten(x: Tensor): Tensor;
export function batchFlatten(x: Tensor): Tensor;

// Slicing
export function sliceAlongFirstAxis(array: Tensor, start: number, size: number): Tensor;
export function sliceAlongLastAxis(array: Tensor, start: number, size: number): Tensor;
export function sliceAlongAxis(array: Tensor, start: number, size: number, axis: number): Tensor;

// Concatenation
export function concatenate(tensors: Tensor[], axis?: number): Tensor;
export function concatAlongFirstAxis(a: Tensor, b: Tensor): Tensor;

// Tiling and random
export function tile(x: Tensor, n: number | number[]): Tensor;
export function randomNormal(shape: Shape, mean?: number, stddev?: number, dtype?: string, seed?: number): Tensor;

// Linear algebra
export function dot(a: Tensor, b: Tensor, activation?: tfc.fused.Activation, bias?: Tensor): Tensor;

// Neural network operations
export function biasAdd(x: Tensor, bias: Tensor, dataFormat?: DataFormat): Tensor;
export function elu(x: Tensor, alpha?: number): Tensor;
export function softsign(x: Tensor): Tensor;
export function hardSigmoid(x: Tensor): Tensor;
export function dropout(x: Tensor, level: number, noiseShape?: number[], seed?: number): Tensor;
export function inTrainPhase<T>(x: () => T, alt: () => T, training?: boolean): T;

// Other
export function oneHot(indices: Tensor, numClasses: number): Tensor;
export function gather(reference: Tensor, indices: number[] | Tensor1D, axis?: number): Tensor;
export function sign(x: Tensor): Tensor;
export function square(x: Tensor): Tensor;
export function pow(x: Tensor, a: Tensor | number): Tensor;

Import

import * as K from '../backend/tfjs_backend';

I/O Contract

Tensor Manipulation

  • countParams: Takes a HasShape object, returns the total number of elements (product of shape dimensions).
  • expandDims: Adds a dimension of size 1 at the specified axis (default -1).
  • repeat: Repeats a rank-2 tensor n times along a new axis 1. Input must be 2D.
  • flatten: Reshapes any tensor to 1D.
  • batchFlatten: Flattens all dimensions except the first (batch). Input must be rank >= 2.

Slicing

  • sliceAlongFirstAxis / sliceAlongLastAxis / sliceAlongAxis: Extract a slice of a tensor along the specified axis. Supports tensors up to rank 6 (first axis) or rank 4 (last axis).

Linear Algebra

  • dot: Multiplies two tensors with rank >= 2. For 2D inputs, uses tfc.fused.matMul with optional fused activation and bias. For higher ranks, follows Theano-style sum-product over last and second-to-last axes.

Neural Network

  • biasAdd: Adds a bias tensor to input, reshaping bias according to rank and data format (channelsFirst or channelsLast).
  • dropout: Sets random entries to zero, scaled by the keep probability.
  • inTrainPhase: Conditionally executes one of two functions based on the training flag.

Usage Examples

import * as K from './backend/tfjs_backend';
import * as tf from '@tensorflow/tfjs-core';

// Count parameters in a tensor
const params = K.countParams(tf.zeros([3, 4, 5])); // 60

// Dot product with fused activation
const a = tf.randomNormal([2, 3]);
const b = tf.randomNormal([3, 4]);
const result = K.dot(a, b, 'relu');

// Bias addition with channels-last format
const x = tf.randomNormal([1, 4, 4, 8]);
const bias = tf.ones([8]);
const biased = K.biasAdd(x, bias, 'channelsLast');

// Dropout during training
const dropped = K.dropout(tf.ones([2, 3]), 0.5);

Related Pages

Page Connections

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