Open In App

Tensorflow.js tf.layers.conv1d() Function

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:

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. 




import * as tf from "@tensorflow/tfjs"
 
  // Creating model
  const geek_Model  =  tf.sequential();
 
  // Adding inputLayer
  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.




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   


Article Tags :