Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.
The .separableConv2d() function is used to determine a 2D complication along with filters that are separable. It executes a depthwise complication which works distinctly on channels pursued by means of a pointwise complexity which is helpful in mixing channels. Moreover, it specifies separability within [1, 2] and 3 dimensions, absolutely not structural separability within 1 and 2 dimensions.
Syntax:
tf.separableConv2d(x, depthwiseFilter, pointwiseFilter, strides, pad, dilation?, dataFormat?)
Parameters:
- x: The stated input tensor which is either of rank 3 or else rank 4 and of shape: [batch, height, width, inChannels]. Moreover, in case the rank is 3, then the batch of size 1 is presumed. It can be of type tf.Tensor3D, tf.Tensor4D, TypedArray, or Array.
- depthwiseFilter: The stated depthwise filter tensor of rank 4 and shape: [filterHeight, filterWidth, inChannels, channelMultiplier]. However, it is utilized in the initial stage. It can be of type tf.Tensor4D, TypedArray, or Array.
- pointwiseFilter: The stated pointwise filter tensor of rank 4 and shape: [1, 1, inChannels * channelMultiplier, outChannels]. However, it is utilized in the second stage of the operation. It can be of type tf.Tensor4D, TypedArray, or Array.
- strides: The stated strides of the complication of shape: [strideHeight, strideWidth]. In case, the stated strides is a singular number, then strideHeight == strideWidth. It can be of type [number, number], or number.
- pad: The stated type of algorithm for padding. It can be of type valid or same.
- Here, for ‘same’ along with stride 1, the output would have an identical size as input, irrespective of the filter size.
- For, ‘valid’ the output shall be smaller than the input in case, the filter size is greater than 1*1×1.
- dilation: The stated dilation. It is optional and is of type [number, number], or number.
- dataFormat: The stated elective string from “NHWC”, or “NCHW”. It specifies the data shape of the stated input as well as output data. The by default value is ‘NHWC’. Moreover, the data here is saved in the following order: [batch, height, width, channels]. It is optional and is of type ‘NHWC’.
Return Value: It returns tf.Tensor3D or tf.Tensor4D.
Example 1:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Defining input tensor const x = tf.tensor4d([1, 2, 3, 4], [1, 1, 2, 2]); // Defining depthwise filter tensor const y = tf.tensor4d([1, 1, 0, 4], [1, 1, 2, 2]); // Defining pointwise filter tensor const z = tf.tensor4d([1, 1, 0, 4], [1, 1, 4, 1]); // Calling separableConv2d() method const result = tf.separableConv2d(x, y, z, 2, 'valid' ); // Printing output result.print(); |
Output:
Tensor [ [ [[34],]]]
Example 2:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Calling separableConv2d() method tf.separableConv2d( tf.tensor4d([1.1, 2.2, 3.3, 4.4], [1, 1, 2, 2]), tf.tensor4d([1.2, 1.1, 0.3, 4.5], [1, 1, 2, 2]), tf.tensor4d([1.4, 1.6, 0.5, 4.8], [1, 1, 4, 1]), 1, 'same' , 1, 'NHWC' ).print(); |
Output:
Tensor [[[[51.6340065 ], [107.0520096]]]]
Reference: https://js.tensorflow.org/api/latest/#separableConv2d