Open In App

Tensorflow.js tf.layers.concatenate() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.

The tf.layers.concatenate() function is used to concatenate an array of inputs.



Syntax:

tf.layers.concatenate()

Parameters:



Return Value: A single tensor, which is the concatenation of all inputs.

Example 1:




// Import the library
import * as tf from "@tensorflow/tfjs"
 
// Inputs
const input1 = tf.input({shape: [3, 2]})
const input2 = tf.input({shape: [3, 2]})
const input3 = tf.input({shape: [3, 2]})
 
// Create new concatenate layer
const concatenateLayer = tf.layers.concatenate();
const output = concatenateLayer.apply([input1, input2, input3]);
 
// Print shape of resulting tensor
console.log(JSON.stringify(output.shape));

 Output:

[pre][null, 3, 6] 

Example 2: 




// Import the library
import * as tf from "@tensorflow/tfjs"
 
// Inputs
const input1 = tf.tensor([-2, 1, 0, 5]);
const input2 = tf.tensor([3, 2, 3, 2]);
const input3 = tf.tensor([4, 3, 1, 2]);
 
// Create new concatenate layer
const concatLayer = tf.layers.concatenate();
const output = concatLayer.apply([input1, input2, input3]);
 
// Print resulting tensor
console.log(output);

 
 Output:

Tensor
    [-2, 1, 0, 5, 3, 2, 3, 2, 4, 3, 1, 2]

 Reference: https://js.tensorflow.org/api/latest/#layers.concatenate


Article Tags :