Open In App

Tensorflow.js tf.conv1d() Function

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:



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

Example 1:




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




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


Article Tags :