Open In App

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:



Return value: It returns the reshape.

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

Example 1:




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




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


Article Tags :