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 .countParams() function is used to find the absolute count of numbers such as float32, int32 in the stated weights.
Syntax:
countParams()
Parameters: This method doesn’t hold any parameter.
Return Value: It returns number.
Example 1:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Creating a model const model = tf.sequential(); // Adding a layer model.add(tf.layers.dense({units: 2, inputShape: [11]})); // Calling setWeights() method model.layers[0].setWeights( [tf.truncatedNormal([11, 2]), tf.zeros([2])]); // Calling countParams() method and also // Printing output console.log(model.layers[0].countParams()); |
Output: Here, truncatedNormal() method is used to create a tf.Tensor along with values that are sampled from a truncated normal distribution, zeros() method is used to create a tf.Tensor along with all the elements that are set to 0 and setWeights() method is used to set the weights.
24
Example 2:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Creating a model const model = tf.sequential(); // Adding layers model.add(tf.layers.dense({units: 1, inputShape: [5], batchSize: 1, dtype: 'int32' })); model.add(tf.layers.dense({units: 2, inputShape: [6], batchSize: 5})); model.add(tf.layers.dense({units: 3, inputShape: [7], batchSize: 8})); model.add(tf.layers.dense({units: 4, inputShape: [8], batchSize: 12})); // Calling setWeights() method model.layers[0].setWeights([tf.ones([5, 1]), tf.zeros([1])]); model.layers[1].setWeights([tf.ones([1, 2]), tf.zeros([2])]); // Calling countParams() method and also // Printing outputs console.log(model.layers[0].countParams()); console.log(model.layers[1].countParams()); console.log(model.layers[2].countParams()); |
Output: Here, ones() method is used to create a tf.Tensor along with all the elements that are set to 1.
6 4 9
Reference: https://js.tensorflow.org/api/latest/#tf.layers.Layer.countParams