Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.Variable class .assign() method

Tensorflow.js tf.Variable class .assign() method

TensorFlow.js is an open-source JavaScript library designed by Google to develop Machine Learning models and deep learning neural networks. tf.Variable class is used to create, track and manage variables in Tensorflow. Tensorflow variables represent the tensors whose values can be changed by running operations on them.

The assign() is the method available in the Variable class which is used to assign the new tf.Tensor to the variable. The new value must have the same shape and dtype as the old Variable value.

Syntax:

assign(newValue)

Parameters:

  • newValue: It’s an object of type tf.Tensor. This new value gets assigned to the variable.

Return Value: It returns void.

Example 1: This example first creates a new tf.Variable object with initial value then assign this variable a new value with same shape and dtype as an initial value.

Javascript




// Defining tf.Tensor object for initial value
initialValue = tf.tensor([[1, 2, 3]])
  
// Defining tf.Variable object 
let x = new tf.Variable(initialValue);
  
// Printing variables dtype
console.log("dtype:",x.dtype)
  
// Printing variable shape
console.log("shape:",x.shape)
  
// Printing the tf.Variable object
x.print()
  
// Defining new tf.Tensor object of same 
// shape and dtype as initial tf.Tensor
newValue = tf.tensor([[5, 8, 10]])
  
// Assigning new value to the variable
x.assign(newValue)
  
// Printing the tf.Variable object
x.print()


Output:

dtype: float32
shape: 1,3
Tensor
     [[1, 2, 3],]
Tensor
     [[5, 8, 10],]

Example 2: This example first creates a new tf.Variable object with initial value then it tries to assign this variable a new value with different shape as the initial value. This will give an error.

Javascript




// Defining tf.Tensor object for initial value
initialValue = tf.tensor([[1, 2],[3, 4]])
  
// Defining tf.Variable object 
let x = new tf.Variable(initialValue);
  
// Printing variables dtype
console.log("dtype:",x.dtype)
  
// Printing variable shape
console.log("shape:",x.shape)
  
// Printing the tf.Variable object
x.print()
  
// Defining new tf.Tensor object of same 
// shape as initial tf.Tensor
newValue = tf.tensor([[5, 6],[10, 11]])
  
// Assigning new value to the variable
x.assign(newValue)
  
// Printing the tf.Variable object
x.print()


Output:

dtype: float32
shape: 2,2
Tensor
    [[1, 2],
     [3, 4]]
Tensor
    [[5 , 6 ],
     [10, 11]]

Reference: https://js.tensorflow.org/api/latest/#tf.Variable.assign

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