 GeeksforGeeks App
Open App Browser
Continue

# Tensorflow.js tf.layers countParams() Method

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: }));`` ` `// Calling setWeights() method``model.layers.setWeights(``    ``[tf.truncatedNormal([11, 2]), tf.zeros()]);`` ` `// Calling countParams() method and also``// Printing output``console.log(model.layers.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: , batchSize: 1, dtype: ``'int32'``}));``model.add(tf.layers.dense({units: 2, inputShape: , batchSize: 5}));``model.add(tf.layers.dense({units: 3, inputShape: , batchSize: 8}));``model.add(tf.layers.dense({units: 4, inputShape: , batchSize: 12}));`` ` `// Calling setWeights() method``model.layers.setWeights([tf.ones([5, 1]), tf.zeros()]);``model.layers.setWeights([tf.ones([1, 2]), tf.zeros()]);`` ` `// Calling countParams() method and also``// Printing outputs``console.log(model.layers.countParams());``console.log(model.layers.countParams());``console.log(model.layers.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```

My Personal Notes arrow_drop_up
Related Tutorials