Friday, October 24, 2025
HomeLanguagesJavascriptTensorflow.js tf.batchNorm() Function

Tensorflow.js tf.batchNorm() Function

Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .batchNorm() function is useful in batch normalization.

Moreover, the mean, variance, scale, including offset can be of two shapes:

  • It can be of shape same as the stated input.
  • In general case, the depth size is the last size of the stated input tensor, so the values can be an tf.Tensor1D of shape [depth].

Syntax:

tf.batchNorm(x, mean, variance, offset?, scale?, varianceEpsilon?)

 

Parameters:

  • x: The stated input Tensor. It can be of type tf.Tensor, TypedArray, or Array.
  • mean: The stated mean tensor. It can be of type tf.Tensor, tf.Tensor1D, TypedArray, or Array.
  • variance: The stated variance tensor. It can be of type tf.Tensor, tf.Tensor1D, TypedArray, or Array.
  • offset: The stated offset tensor. It is optional and can be of type tf.Tensor, tf.Tensor1D, TypedArray, or Array.
  • scale: The stated scale tensor. It is optional and can be of type tf.Tensor, tf.Tensor1D, TypedArray, or Array.
  • varianceEpsilon: The stated minor float number in order to escape division by 0. It is optional and is of type number.

Return Value: It returns tf.Tensor.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining input tensor
const a = tf.tensor1d([1, 5, 3]);
  
// Defining mean
const b = tf.tensor1d([1, 1, 2]);
  
// Defining variance
const c = tf.tensor1d([1, 0, 1]);
  
// Calling batchNorm() function
tf.batchNorm(a, b, c).print();


Output:

Tensor
    [0, 126.4911041, 0.9995003]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining input tensor
const a = tf.tensor1d([1, 5, 3]);
  
// Defining mean
const b = tf.tensor1d([1, 1, 2]);
  
// Defining variance
const c = tf.tensor1d([1, 0, 1]);
  
// Defining offset
const d = tf.tensor1d([1, 6, 2]);
  
// Defining scale
const e = tf.tensor1d([1, 0, 1]);
  
// Calling batchNorm() function
a.batchNorm(b, c, d, e, 9).print();


Output:

Tensor
    [1, 6, 2.3162277]

Reference: https://js.tensorflow.org/api/latest/#batchNorm

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS