Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.layers computeOutputShape() Method

  • Last Updated : 27 Jul, 2021

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 .computeOutputShape() function is used to enumerate the output shape of the stated layer. And it presumes that the layer will be created in order to match the supplied input shape.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Syntax:

computeOutputShape(inputShape)

Parameters:



  • inputShape: It is the stated shape i.e. set of integers or a list of sets of shape. Moreover the Shape tuples can contain void in favor of free sizes, in place of an integer. It can of type ((null | number)[]|(null | number)[][]).

Return Value: It returns (null | number)[]|(null | 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: 1, inputShape: [3]}));
  
// Defining inputShape
const inputShape = [6, 2, 6];
  
// Calling computeOutputShape() method with its 
// parameter
const val = model.layers[0].computeOutputShape(inputShape);
  
// Printing output
console.log(val);

Output:

6,2,1

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: [3]}));
model.add(tf.layers.dense({units: 5}));
  
// Defining inputShape
const inputShape1 = [6, 2, 6, null];
const inputShape2 = [6.5, 2.6, 9.1, NaN];
  
  
// Calling computeOutputShape() method with its 
// parameter
const val1 = model.layers[0].computeOutputShape(inputShape1);
const val2 = model.layers[1].computeOutputShape(inputShape2);
  
// Printing output
console.log(val1);
console.log(val2);

Output:

6,2,6,1
6.5,2.6,9.1,5

Reference: https://js.tensorflow.org/api/latest/#tf.layers.Layer.computeOutputShape




My Personal Notes arrow_drop_up
Recommended Articles
Page :