Open In App
Related Articles

Tensorflow.js tf.concat() Function

Improve Article
Improve
Save Article
Save
Like Article
Like

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.concat() function is used to concatenate the list of specified Tensors along the given axis.

Syntax:

tf.concat (tensors, axis)

Parameters: This function accepts two parameters which are illustrated below:

  • tensors: It is a list of specified tensors to concatenate.
  • axis: It is the axis along which the concatenation process is being performed. It is an optional parameter and its default value is 0.

Return Value: It returns a Tensor of concatenated tensors. 

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing two tensors to concatenate
const A = tf.tensor1d([0, 2, 4]);
const B = tf.tensor1d([1, 3, 5]);
  
// Calling the .concat() function over
// the above tensors as its parameters
A.concat(B).print();


Output:

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

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing three 2-D tensors to concatenate
const A = tf.tensor2d([[0, 2], [1, 3]]);
const B = tf.tensor2d([[4, 6], [5, 7]]);
const C = tf.tensor2d([[8, 10], [9, 11]]);
  
// Initializing axis parameter
const axis = 1;
  
// Calling the .concat() function over
// the above tensors and axis as its parameters
tf.concat([A, B, C], axis).print();


Output:

Tensor
   [[0, 2, 4, 6, 8, 10],
    [1, 3, 5, 7, 9, 11]]

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 22 Apr, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials