Open In App

Tensorflow.js tf.layers.upSampling2d() Function

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

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js.

The tf.layers.upSampling2d() function is used to repeat the rows and columns of the data by size[0] and size[1] respectively. upSampling2d is the Up-Sampling layer for 2D inputs.

 Syntax:

tf.layers.upSampling2d( args )

Parameters:

  • args: It accepts the object with the following fields:
    • size: It is an array of numbers. It is the upSampling factor for rows and columns.
    • dataFormat: It is the format of the data, which determines the ordering of the dimension in the inputs.
    • interpolation: It defines the interpolation mechanism. It should be ‘nearest’ or bilinear’. Default value is ‘nearest’.
    • inputShape: It should be an array of numbers. This field is used to create an input layer which is used to be inserted before this layer.
    • batchInputShape: It should be an array of numbers. This field will be used in place of inputShape for creating the input layer which is used to insert before this layer.
    • batchSize: It should be a number. In the absence of batchInputShape this field is used to create batchInputShape with inputShape. batchInputShape : [ batchSize ,  …inputShape].
    • dtype: If this layer is used as the input layer, then this field is used as data-type for this layer.
    • name: It should be a string type. this field defines the name for this layer.
    • trainable: It should be boolean. This field defines whether the weights of this layer are trainable with fit or not.
    • weights: This should be a tensor that defines the initial weight value for this layer.
    • inputDType: This is a data type that is used for Legacy support.

Parameters: It returns UpSampling2D.

Example 1:

Javascript




// Import the header file
import * as tf from "@tensorflow/tfjs"
  
// Creating upSampling2d layer
const upSampling = tf.layers.upSampling2d({
    size: [2, 1],
    batchInputShape: [2, 3, 5, 5]
});
  
// Create an input with 2 time steps.
const input = tf.input({shape: [2, 3, 3]});
const output = upSampling.apply(input);
  
// Printing the Shape of file
console.log(JSON.stringify(output.shape));


Output:

[null, 4, 3, 3]

Example 2:

Javascript




// Import Header file
import * as tf from "@tensorflow/tfjs"
  
// Creating input layer
const inputShape = [1, 1, 1, 2];
const input = tf.ones(inputShape);
  
// Creating upSampling layer
const layer = tf.layers.upSampling2d({size: [1,2]});
  
// Printing tensor
const output = layer.apply(input);
output.print();


Output:

Tensor
    [[[[1, 1],
       [1, 1]]]]

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



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

Similar Reads