Open In App

Tensorflow.js tf.conv2d() Function

Last Updated : 31 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Tensorflow.js is a javascript library developed by Google to run and train machine learning models in the browser or in Node.js. 

The tf.conv2d() function is used to compute 2d convolutions over the given input. In a deep neural network, we use this convolution layer which creates a convolution kernel which when applied to the input layers produces a tensor of outputs. 

Syntax:

tf.conv2d (x, filter, strides, pad, dataFormat?, 
        dilations?, dimRoundingMode?) 

Parameters:

  • x: A tensor of rank 3 and rank 4 with shape [batch, height, width, inChannels] is given. It can be a 3D tensor, 4D tensor or a typed array.
  • filter: Filter is of rank 4 with shape parameters [filterHeight, filterWidth, inDepth, outDepth] is passed. It needs to be a 4D tensor, nested array or a typed array.
  • strides ([number, number]|number): Strides of the convolution : [strideHeight, strideWidth].
  • pad: The type of padding algorithm.
    • Same: Regardless of filter size, output will be of same size as input.
    • valid: Output will be smaller than input if filter is larger than 1×1.
    • It can also be a number or conv_util.ExplicitPadding.
  • dataFormat: Out of two strings it can be either: “NHWC”, “NCHW”. With the default format “NHWC”, the data is stored in the order of: [batch, height, width, channels]. Data format of the input and output data need to be specified. This is optional.
  • dilations: The dilation rate is two tuples of integers that check the rate of the convolution.[dilationHeight ,dilationWidth]is passed as parameters. This is optional.
  • dimRoundingMode: The string format will be either ‘ceil’ or ’round’ or ‘floor’. This is optional.

 

Example 1: Here we take a 4-dimensional tensor input and another 4d kernel and then apply convolution which results in the output tensor. The shapes are also printed along with the output tensor.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Input tensor
const x = tf.tensor4d([[
  [[2], [1], [2], [0], [1]],
  [[1], [3], [2], [2], [3]],
  [[1], [1], [3], [3], [0]],
  [[2], [2], [0], [1], [1]],
  [[0], [0], [3], [1], [2]], ]]);
console.log('Shape of the input:',x.shape);
  
// Kernel has been set
const kernel = tf.tensor4d([
     [ [[2, 0.1]], [[3, 0.2]] ],
     [ [[0, 0.3]], [[1, 0.4]] ],
]);
  
console.log('Shape of the kernel:',kernel.shape);
  
// Output tensor after convolution
let out = tf.conv2d(x, kernel, 
    strides = [1, 1, 1, 1], 'same');
  
out.print();
console.log('Shape of the output:',out.shape)


Output:

Shape of the input: 1,5,5,1
Shape of the kernel: 2,2,1,2
Tensor
    [[[[10, 1.9000001],
       [10, 2.2      ],
       [6 , 1.6      ],
       [6 , 2        ],
       [2 , 1        ]],

      [[12, 1.4      ],
       [15, 2.2      ],
       [13, 2.7      ],
       [13, 1.7      ],
       [6 , 0.3      ]],

      [[7 , 1.7      ],
       [11, 1.3      ],
       [16, 1.3000001],
       [7 , 1        ],
       [0 , 0.3      ]],

      [[10, 0.6      ],
       [7 , 1.4      ],
       [4 , 1.5      ],
       [7 , 1.4000001],
       [2 , 0.7      ]],

      [[0 , 0        ],
       [9 , 0.6      ],
       [9 , 0.5      ],
       [8 , 0.5      ],
       [4 , 0.2      ]]]]
Shape of the output: 1,5,5,2

Example 2: Convolutions play an important role in designing the architecture of deep learning model. In this example, we create a function and within define a sequential model. After this we add model layers using tf.layers.conv2d() with input shape, filter, kernel, padding as its parameters. Then after subsequent maxpooling, flattening and compiling we return the model.

Javascript




// Define the model architecture
function buildModel() {
    const model = tf.sequential();
  
    // Add the model layers starting
    // with convolution layers
    model.add(tf.layers.conv2d({
        inputShape: [28, 28, 1],
        filters: 8,
        kernelSize: 5,
        padding: 'same',
        activation: 'relu'
    }));
  
    model.add(tf.layers.maxPooling2d({
        poolSize: 2,
        strides: 2
    }));
  
    // Again we set  another convolution layer
    model.add(tf.layers.conv2d({
        filters: 16,
        kernelSize: 5,
        padding: 'same',
        activation: 'relu'
    }));
      
    model.add(tf.layers.maxPooling2d({
        poolSize: 3,
        strides: 3
    }));
  
    const numofClasses = 10;
    model.add(tf.layers.flatten());
    model.add(tf.layers.dense({
        units: numofClasses,
        activation: 'softmax'
    }));
  
    // Compile the model
    model.compile({
        optimizer: 'adam',
        loss: 'categoricalCrossentropy',
        metrics: ['accuracy']
    });
  
    return model;
}
const jsmodel = buildModel()
jsmodel.summary()


Output:

_________________________________________________________________
Layer (type)                 Output shape              Param #   
=================================================================
conv2d_Conv2D1 (Conv2D)      [null,28,28,8]            208       
_________________________________________________________________
max_pooling2d_MaxPooling2D1  [null,14,14,8]            0         
_________________________________________________________________
conv2d_Conv2D2 (Conv2D)      [null,14,14,16]           3216      
_________________________________________________________________
max_pooling2d_MaxPooling2D2  [null,4,4,16]             0         
_________________________________________________________________
flatten_Flatten1 (Flatten)   [null,256]                0         
_________________________________________________________________
dense_Dense1 (Dense)         [null,10]                 2570      
=================================================================
Total params: 5994
Trainable params: 5994
Non-trainable params: 0
_________________________________________________________________

Reference: https://js.tensorflow.org/api/3.6.0/#layers.conv2d



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

Similar Reads