Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.buffer() Function

Tensorflow.js tf.buffer() 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.

The tf.buffer() function is used to create an empty Tensor Buffer for the specified data type and shape. The values are set in the created buffer using buffer.set() function.

Syntax:

tf.buffer (shape, dtype, values)

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

  • shape: An array of integers which defines the shape of output tensor.
  • dtype: The data type of the created buffer. It’s defaults value is ‘float32’. This parameter is optional.
  • values: The values for the created buffer. It’s default values is zeros. This parameter is optional.

Return Value: This function does not return any values as it create the buffer only.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating a buffer of [2, 2] shape
const buffer = tf.buffer([2, 2]);
  
// Getting the created buffer in the
// form of Tensor of zeros values 
// as no values are set in the buffer
buffer.toTensor().print();


Output:

Tensor
   [[0, 0],
    [0, 0]]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating a buffer of [3, 3] shape 
const buffer = tf.buffer([3, 3]);
  
// Setting values in the created buffer
// at particular indices
buffer.set(10, 2, 0);
buffer.set(15, 0, 1);
buffer.set(20, 1, 2);
  
// Getting the buffer in the form of Tensor
// along with the set values
buffer.toTensor().print();


Output:

Tensor
   [[0 , 15, 0 ],
    [0 , 0 , 20],
    [10, 0 , 0 ]]

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

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