Tensorflow.js tf.TensorBuffer Class .set() Method

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.TensorBuffer class .set() function is used to set a given value in the buffer at a specified location.
Syntax:
set (value, ...locations)
Parameters: This function accepts two parameters which are illustrated below:
- value: The specified value to set.
- locations: The specified location indices.
Return Value: It does not return any value.
Example 1:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs"Ā Ā // Creating a buffer of 2*2 dimensions const buffer = tf.buffer([2, 2]);Ā Ā Ā // Setting values at particular indices.Ā buffer.set(5, 0, 0);Ā buffer.set(10, 1, 0);Ā Ā Ā // Converting the above buffer // back to a tensor value to print buffer.toTensor().print(); |
Output:
Tensor
[[5 , 0],
[10, 0]]
Example 2:
Javascript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs"Ā Ā // Creating a buffer of 3*3 dimensions const buffer = tf.buffer([3, 3]);Ā Ā Ā // Setting values at particular indices.Ā buffer.set(5, 0, 0);Ā buffer.set(10, 0, 1);Ā buffer.set(15, 1, 0);Ā buffer.set(20, 1, 1);Ā buffer.set(25, 2, 0);Ā buffer.set(30, 2, 1);Ā buffer.set(35, 2, 2);Ā Ā Ā // Converting the above buffer // back to a tensor value to print buffer.toTensor().print() |
Output:
Tensor
[[5 , 10, 0 ],
[15, 20, 0 ],
[25, 30, 35]]
Reference: https://js.tensorflow.org/api/latest/#tf.TensorBuffer.set
Whether youāre preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!


