Skip to content
Related Articles

Related Articles

Improve Article

Tensorflow.js tf.layers.reshape() Function

  • Last Updated : 04 Jul, 2021

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.

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:

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :