Skip to content
Related Articles
Open in App
Not now

Related Articles

Tensorflow.js tf.split() Function

Improve Article
Save Article
Like Article
  • Last Updated : 10 May, 2021
Improve Article
Save Article
Like Article

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, geeks] = tf.split(x, 2, 1);
  
gfg.print();
  
geeks.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, geeks] = tf.split(x, [1, 2, 1], 1);
  
gfg.print();
  
gfg1.print();
  
geeks.print();


Output:

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

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

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!