Open In App

Tensorflow.js tf.layers.conv2dTranspose() Function

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.conv2dTranspose() function is used to transposed convolutions which generally arises from the desire to use a transformation going in the opposite direction of a normal convolution. When using this layer as the first layer in a model, it provides the configuration inputShape, e.g, inputShape: [ 64, 64, 5 ] for 64 x 64 RGB pictures in dataFormat: ‘channelsLast’.



Syntax:

tf.layers.conv2dTranspose( args )

Parameters: 



Return Value: It returns Conv2DTranspose

Example 1:




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

Output:

[null,11,12,3]

Example 2:




// 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.conv2dTranspose({
    filters: 2, kernelSize: 2,
    batchInputShape: [1, 2, 3]
});
 
// Printing tensor
const output = layer.apply(input);
output.print();

Output:

Tensor
    [[[[0.081374  , -0.2834765],
       [-0.1283467, -0.2375581]],

      [[-0.791486 , 0.2895283 ],
       [-0.2392025, -0.1721524]]]]

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


Article Tags :