Wednesday, July 3, 2024
HomeLanguagesJavascriptTensorflow.js tf.layers.reshape() Function

Tensorflow.js tf.layers.reshape() Function

Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The tf.layers.reshape() function is used to Reshape an input to a certain shape.

Syntax:

tf.layers.reshape(args) 

Parameters: This function takes the args object as a parameter which can have the following properties:

  • targetShape: It is a number which does not include the batch axis.
  • inputShape: It is a number which is used to create an input layer to insert before this layer.
  • batchInputShape: It is a number which is used to create an input layer to insert before this layer.
  • batchSize: It is a number is used to construct the batchInputShape.
  • dtype: data-type for this layer.
  • name: It is a string for this layer.
  • trainable: It is a boolean in which whether the weights of this layer are updatable by fit or not.
  • weights: Initial weight values of the layer.
  • inputDtype: It is for Legacy support. Do not use for new code.

Return value: It returns the reshape.

The below examples demonstrates the reshaping of layers using the tf.layers.reshape() function.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining the tensor input elements 
const input = tf.input({shape: [2, 6]});
  
// Calling the layers.reshape ( ) function
const reshapeLayer = tf.layers.reshape({targetShape: [3, 9]});
  
// Inspect the inferred output shape of the
// Reshape layer, which equals `[null, 3, 9]`. 
// (The 1st dimension is the undermined batch size.)
console.log(JSON.stringify(
    reshapeLayer.apply(input).shape));


Output:

[null, 3, 9]

Example 2: In this example we are talking about the reshaping of layers.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining the tensor input elements 
const input = tf.input({shape: [4, 8]});
  
// Calling the layers.reshape ( ) function
const reshapeLayer = 
    tf.layers.reshape({targetShape: [4, 8]});
  
// Inspect the inferred output shape of
// the Reshape layer, which equals `[null, 4, 8]`. 
// (The 1st dimension is the undermined batch size.)
console.log(JSON.stringify(
    reshapeLayer.apply(input).shape));


Output:

[null, 4, 8]

Reference: https://js.tensorflow.org/api/latest/#layers.reshape

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments