Open In App

Tensorflow.js tf.layers.average() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or Node.js.

The tf.layers.average() function is used to calculate average of every element when multiple arrays are provided as inputs.



Syntax:

tf.layers.average (inputShape?, batchInputShape?, batchSize?, 
        dtype?, name?, trainable?, weights?, inputDType? )

Parameters:



Return Value: It returns average.

Example 1:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
const input1 = tf.input({shape: [33, 3]});
const input2 = tf.input({shape: [33, 3]});
const averageLayer = tf.layers.average();
const average = averageLayer.apply([input1, input2]);
console.log(JSON.stringify(average.shape));

Output:

[null, 33, 3]

Example 2:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
const input1 = tf.input({shape: [4, 4]});
const input2 = tf.input({shape: [4, 4]});
const input3 = tf.input({shape: [4, 4]});
const input4 = tf.input({shape: [4, 4]});
const input5 = tf.input({shape: [4, 4]});
const input6 = tf.input({shape: [4, 4]});
 
 
 
const averageLayer = tf.layers.average();
const average = averageLayer.apply([input1,
    input2, input3, input4, input5, input6 ]);
console.log(JSON.stringify(average.shape));

Output:

[null, 4, 4]

Reference: https://js.tensorflow.org/api/latest/#layers.average


Article Tags :