Skip to content
Related Articles

Related Articles

Improve Article

Tensorflow.js tf.layers.conv1d() Function

  • Difficulty Level : Medium
  • Last Updated : 26 Sep, 2021
Geek Week

Tensorflow.js is a javascript library developed by Google to run and train machine learning models in the browser or in Node.js. Tensorflow.js tf.layers.conv1d() function is used to create convolution layer. It is used to applied 1d convolution to the input data. The convolutional layer is used to make a filter which is used to filter input data in the desired output.

Syntax:

tf.layers.conv1d(args);

Parameters: This function accepts following parameters:

  • args: This is object type that contains following values:
    • filters: It is the number that defined the dimension of the output tensor. It the number of  filters in the convolution.
    • kernelSize: It define the convolution window to the convolution layers which means subset of input data takes at once.
    • strides: It define the unit to move in input data by the convolution layer in each dimensions.
    • padding: It define the padding to the convolution layer which means to amount to data added to the input data during processing. It’ s value can be ‘valid’ , ‘same’, and ‘causal’.
    • dataFormat: It define the data format of the input data as data is in ‘channelsFirst’ or in ‘channelsLast’.
    • dilationRate: It is used to dilate the convolution in each dimension. It is used to define the space in between the values in kernel.
    • activation: It define the activation function to use in for the input data.
    • useBias: It is used for making decision for using bias vector or not.
    • kernelInitializer: It is the initial weight for the kernel in convolution layer. It define the way to set the initial random weights of convolution layers.
    • biasInitializer: It is the initial weight for the bias vector of convolution layer.
    • kernelConstraint: It is the constraint for the convolution layer kernel weight which is used to increase the efficiency.
    • biasConstraint: It is the constraint for the convolution layer bias vector to increase the efficiency.
    • kernelRegularizer: It is the regularizer function that is applied to matrix of the weight of the kernel to regularize it.
    • biasRegularizer: It the regularizer function that is applied to the bias vector to regularize it.
    • activityRegularizer: it is the function that is applied to the activation for regularize purpose.
    • inputShape: It declared that this layer accepts the the input layer of this shape. it is only used for the input layer.
    • batchInputShape: It define the batch size of input layer. It is used for the input layer.
    • batchSize: It is used as supplementry to the batchInputShape in process of making input layer.
    • dtype: It define the data-type for this layer. ‘float32’ is the default value for layer.
    • trainable: It is used to make layer trainable of function or not.
    • weights: It is the initial weight value fo this layer.
    • inputDtype: It is used deciding the data-type of the input layer.

Returns: Conv1D

Example 1: In this example, we will create sequential model and add the 1d convolution layer to it with filter ,kernelSize ,inputShape and activation. At last we compile our model with layers and see the summary of it. 



Javascript




import * as tf from "@tensorflow/tfjs"
 
  // Craating model
  const geek_Model  =  tf.sequential();
 
  // Adding inputLayer layer
  const geek_config = {inputShape: [7,4]};
  const geek_layer1 = tf.layers.inputLayer(geek_config);
  geek_Model.add(geek_layer1);
 
  // Adding convolution layer
  const geek_config2 = {
    filters:10,kernelSize:7                        
   ,inputShape:[28,1],
    activation: 'relu'};
  const geek_layer2 = tf.layers.conv1d(geek_config2);
   //console.log(y.shape);
   geek_Model.add(geek_layer2);
 
  // Adding dense layer
  const geek_config3 = {
    units:7,
    activation: 'softmax'
  };
  const geek_layer3 = tf.layers.dense(geek_config3);
  geek_Model.add(geek_layer3)
 
  // Compiling our model
  const geek_config4 = {
    optimizer: 'sgd',
    loss: 'meanSquaredError'
  };
  geek_Model.compile(geek_config4);
 
  // Printing our summary
  geek_Model.summary()

Output:

_________________________________________________________________
Layer (type)                 Output shape              Param #   
=================================================================
input1 (InputLayer)          [null,7,4]                0         
_________________________________________________________________
conv1d_Conv1D1 (Conv1D)      [null,1,10]               290       
_________________________________________________________________
dense_Dense1 (Dense)         [null,1,7]                77        
=================================================================
Total params: 367
Trainable params: 367
Non-trainable params: 0
_________________________________________________________________

Example 2: In this example, we will see how the convolution layer filter the input data as per the configuration. Here we make convolution layer with the inputShape ,filter , kernelSize ,activation and compile the model with it. With the convolution layer filtered we use predict function for the input data.

Javascript




import * as tf from "@tensorflow/tfjs"
 
// Input Layer for model
const geek_config = {shape:[3,4]};
const geek_InputL = tf.input(geek_config);
console.log(`Input layer shape : ${geek_InputL.shape}`);
 
// Convolution For the model 
const geek_config2 = {
  filters:2,kernelSize:2                        
 ,inputShape:[3,4],
  activation: 'sigmoid'};
const geek_convLayer = tf.layers.conv1d(geek_config2).apply(geek_InputL);
console.log(`Convolution layer shape : ${geek_convLayer.shape}`);
 
// Adding layer to the model
const geek_config3 = {inputs:geek_InputL, outputs:geek_convLayer};
const model = tf.model(geek_config3);
// Compiling the model
const geek_config4 = {optimizer:'sgd',loss:'meanSquaredError'};
model.compile(geek_config4);
 
// Predicting the value for the input
const geek_Test = tf.randomUniform([3,3,4]);
model.predict(geek_Test).print();

Output: 

Input layer shape : ,3,4
Convolution layer shape : ,2,2
Tensor
    [[[0.5468831, 0.4990641],
      [0.3059803, 0.4743758]],

     [[0.4450175, 0.4848864],
      [0.3678558, 0.4276305]],

     [[0.4802476, 0.5687023],
      [0.4083693, 0.4854257]]]

Reference: https://js.tensorflow.org/api/3.6.0/#layers.conv1d   

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :