Thursday, September 4, 2025
HomeLanguagesJavascriptTensorflow.js tf.Variable class

Tensorflow.js tf.Variable class

TensorFlow is an open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks in the browser or node environment.

The tf.Variable class is used to make a new mutable tf.Tensor and is provided with an assign value that copies the tf.Tensor to this new variable containing the same shape and dtype.

tf.Variable class extends tf.Tensor class.

Syntax:

tensorflow.Variable(initialTensor)

Parameters:

  • initialTensor: It is the initial tensor that is assigned to the Variable class object.

Return Value: It returns nothing (i.e. void).

Example 1: This example only uses the initial value to create tf.Variable object. No optional parameters are passed.

Javascript




// Defining tf.Tensor object for initial value
initialValue = tf.tensor([[1, 2, 3]])
 
// Defining tf.Variable object
const x = new tf.Variable(initialValue);
 
// Checking variables dtype
console.log("dtype:", x.dtype)
 
// Checking variable shape
console.log("shape:", x.shape)
 
// Printing the tf.Variable object
x.print()


Output:

dtype: float32
shape: 1,3
Tensor
     [[1, 2, 3],]

Example 2: This example uses optional parameters along with the initial value to create a Variable object.

Javascript




// Defining tf.Tensor object for initial value
initialValue = tf.tensor([[1, 2, 3]])
 
// Defining tf.Variable object
const x = new tf.Variable(initialValue,
    false, 'example_variable', 'int32');
 
// Checking if variable is trainable
console.log("Is trainable:", x.trainable)
 
// Checking variables dtype
console.log("dtype:", x.dtype)
 
// Checking variable shape
console.log("shape:", x.shape)
 
// Checking variable name
console.log("Name:", x.name)
 
// Printing the tf.Variable object
x.print()


Output:

Is trainable: false
dtype: int32
shape: 1,3
Name: example_variable
Tensor
     [[1, 2, 3],]

Example 3: This example creates a tf.Variable object using initial value then again adds the initial value to the Variable.

Javascript




// Defining tf.Tensor object for initial value
initialValue = tf.tensor([[1, 2, 3]])
 
// Defining tf.Variable object
const x = new tf.Variable(initialValue);
 
// Printing the tf.Variable object
x.print()
 
// Adding initial value Tensor to Variable
result = x.add(initialValue)
 
// Printing result
result.print()


Output:

Tensor
     [[1, 2, 3],]
Tensor
     [[2, 4, 6],]
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

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS