Open In App

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:

Syntax:



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

 

Parameters:

Return Value: It returns tf.Tensor.

Example 1:




// 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:




// 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


Article Tags :