Thursday, October 23, 2025
HomeLanguagesJavascriptTensorflow.js tf.split() Function

Tensorflow.js tf.split() 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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js.

The tf.split() function is used to split a tf.tensors into sub tensors.

Syntax:

tf.split (x, numOrSizeSplits, axis?)

Parameters:

  • x: The input tensor to split.
  • numOrSizeSplits: It can be a number indicating the number of splits or can be the array in which sizes are provided for each output tensor
  • axis: It is an axis of the dimension along which to split.

Return Value: It returns tf.Tensor[].

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
const x = tf.tensor2d([10, 20, 50, 60,
                       30, 40, 70, 80], [2, 4]);
  
// Split the tensor
const [gfg, neveropen] = tf.split(x, 2, 1);
  
gfg.print();
  
neveropen.print();


Output:

Tensor
    [[10, 20],
     [30, 40]]
Tensor
    [[50, 60],
     [70, 80]]

Example 2: In this example, making the split with axis as the 2nd parameter as an array.

Javascript




const x = tf.tensor2d([10, 30, 50, 70, 20, 40, 60, 80], [2, 4]);
  
const [gfg, gfg1, neveropen] = tf.split(x, [1, 2, 1], 1);
  
gfg.print();
  
gfg1.print();
  
neveropen.print();


Output:

Tensor
    [[10],
     [20]]
Tensor
    [[30, 50],
     [40, 60]]
Tensor
    [[70],
     [80]]

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS