Skip to content
Related Articles

Related Articles

Tensorflow.js tf.linalg.bandPart() Function

View Discussion
Improve Article
Save Article
  • Last Updated : 12 May, 2021

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.linalg.bandPart() function is used to calculate the band part of the given tensor.

Syntax:

tf.linalg.bandPart (a, numLower, numUpper)

Parameters:

  • a: it is tf.tensor to be passed.
  • numLower: Number of subdiagonals to keep. If negative, keep entire lower triangle
  • numUpper: Number of subdiagonals to keep. If negative, keep entire upper triangle

Return Value: It returns tf.Tensor1D.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
const x = tf.tensor2d([[ 0,  1,  2, 3],
                     [ 4,  0,  1, 2],
                     [ 3,  1,  0, 1],
                     [ 3,  1,  1, 0]]);
  
let y = tf.linalg.bandPart(x, 1, 3);
y.print();

Output:

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

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
const x = tf.tensor2d([[ 0,  1,  2, 3],
                     [ 4,  0,  1, 2],
                     [ 3,  1,  0, 1],
                     [ 3,  1,  1, 0]]);
  
let k = tf.linalg.bandPart(x, -1, -3);
k.print();

Output:

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

Reference: https://js.tensorflow.org/api/latest/#linalg.bandPart

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!