Open In App

Tensorflow.js tf.conv2d() Function

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:



 

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.




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




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


Article Tags :