Tensorflow.js tf.layers countParams() Method
Last Updated :
22 Apr, 2022
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
import * as tf from "@tensorflow/tfjs"
const model = tf.sequential();
model.add(tf.layers.dense({units: 2, inputShape: [11]}));
model.layers[0].setWeights(
[tf.truncatedNormal([11, 2]), tf.zeros([2])]);
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
import * as tf from "@tensorflow/tfjs"
const model = tf.sequential();
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}));
model.layers[0].setWeights([tf.ones([5, 1]), tf.zeros([1])]);
model.layers[1].setWeights([tf.ones([1, 2]), tf.zeros([2])]);
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...