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