Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 12 May, 2021

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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :