Monday, September 23, 2024
Google search engine
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

Recent Comments