Open In App

Tensorflow.js tf.layers computeOutputShape() 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 .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:



Return Value: It returns (null | number)[]|(null | number)[][].

Example 1:




// 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:




// 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


Article Tags :