Open In App

Tensorflow.js tf.conv1d() Function

Last Updated : 01 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Introduction: 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 .conv1d() function is used to determine a 1D convolution upon the stated input tensor.

Syntax:

tf.conv1d(x, filter, stride, pad, dataFormat?, dilation?, dimRoundingMode?)

Parameters:

  • x: The stated input tensor which is either of rank 3 or else rank 2 and of shape: [batch, height, width, inChannels]. Moreover, in case the rank is 2, then the batch of size 1 is presumed. It can be of type tf.Tensor2D, tf.Tensor3D, TypedArray, or Array.
  • filter: The stated filter tensor of rank 3 and shape: [filterHeight, filterWidth, depth]. It can be of type  tf.Tensor3D, TypedArray, or Array.
  • strides: The stated number of intakes with the help of which the stated filter is shifted right at each step. It is of type number.
  • pad: The stated type of algorithm for padding. It can be of type valid, same, number, or conv_util.ExplicitPadding.
    1. Here, for same and stride 1, the output would have identical size as input, irrespective of the filter size.
    2. For, ‘valid’ the output shall be smaller than the input in case, the filter size is larger than 1*1×1.
  • dataFormat: The stated elective string out of “NWC”, or “NCW”. The by default value is “NWC”, the information is kept in the sequence of [batch, in_width, in_channels]. Moreover, just “NWC” is currently favored. It is optional and is of type ‘NWC’, or ‘NCW’.
  • dilations: The stated dilation rates where input values are sampled in atrous convolution. The by default value is 1. And in case its greater than 1, then the stride should be 1. It is optional and is of type number.
  • dimRoundingMode: A stated string out of ‘ceil’, ’round’, or ‘floor’. In case, no value is provided, then the by default value is truncate. It is optional and is of type ceil, round or floor.

Return Value: It returns tf.Tensor2D or tf.Tensor3D.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining input tensor
const x = tf.tensor3d([1, 2, 3, 4], [2, 2, 1]);
  
// Defining filter tensor
const y = tf.tensor3d([1, 1, 0, 4], [1, 1, 4]);
  
// Calling conv1d() method
const result = tf.conv1d(x, y, 2, 'valid');
  
// Printing output
result.print();


Output:

Tensor
    [ [[1, 1, 0, 4 ],],

      [[3, 3, 0, 12],]]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling conv1d() method with 
// all its parameters
tf.tensor3d([1.1, 2.2, 3.3, 4.4], [2, 2, 1]).conv1d(
 tf.tensor3d([1.3, 1.2, null, -4], [1, 1, 4]),
             2, 0, 'NWC', 1, 'ceil').print();


Output:

Tensor
    [[[1.4299999, 1.3200001, 0, -4.4000001 ],
      [0        , 0        , 0, 0          ]],

     [[4.29     , 3.96     , 0, -13.1999998],
      [0        , 0        , 0, 0          ]]]

Reference: https://js.tensorflow.org/api/latest/#conv1d



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

Similar Reads