Open In App

Tensorflow.js tf.layers.depthwiseConv2d() Function

Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js.

The tf.layers.depthwiseConv2d() function is used to apply the depthwise separable 2D convolution operation on data.



Syntax:

tf.layers.depthwiseConv2d(args)

Parameters: It accepts the args object which can have the following properties:



Returns: It returns an object (DepthwiseConv2D).

Example 1:




import * as tf from "@tensorflow/tfjs";
 
const input = tf.input({ shape: [4, 4, 4] });
const depthwiseConv2DLayer = tf.layers.depthwiseConv2d({
    kernelSize: 2,
    depthMultiplier: 2
});
 
const output = depthwiseConv2DLayer.apply(input);
 
const model = tf.model({ inputs: input, outputs: output });
model.predict(tf.ones([1, 4, 4, 4])).print();

Output:

Tensor
   [[[[0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304]],
     [[0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304]],
     [[0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],

Example 2:




import * as tf from "@tensorflow/tfjs";
const input = tf.input({ shape: [4, 4, 1] });
const depthwiseConv2DLayer = tf.layers.depthwiseConv2d({
    kernelSize: 3
});
 
const output = depthwiseConv2DLayer.apply(input);
const model = tf.model({ inputs: input, outputs: output });
const x = tf.tensor4d(
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
    [1, 4, 4, 1]
);
 
model.predict(x).print();

Output:

Tensor
   [[[[2.8932226],
      [2.8629632]],
     [[2.7721865],
      [2.7419279]]]]

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


Article Tags :