Open In App

Tensorflow.js tf.layers computeOutputShape() Method

Last Updated : 22 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads