Open In App

Tensorflow.js tf.variable() 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. It helps developers to develop ML models in JavaScript, and use ML directly in the browser or in Node.js.

The tf.variable() function is used to create a new variable with the provided initial value.



Syntax:

tf.variable(initialValue, trainable, name, dtype)

Parameters:



Return Value: This function returns tf.variable.

Example 1:




// Creating and initializing a new variable
var val = tf.variable(tf.tensor2d(
    [8, 2, 5, 6],
    [2, 2]
));
 
// Printing the tensor
val.print()

Output:

Tensor
   [[8, 2],
    [5, 6]]

Example 2:




// Creating and initializing a new variable
var val = tf.variable(tf.tensor([1, 2, 5, 6]));
 
// Printing the tensor
val.print()

Output:

Tensor
   [1, 2, 5, 6]

Example 3:




// Creating and initializing a new variable
const x = tf.variable(tf.tensor([1, 2, 3]),
    true,"gfg",'complex64');
 
// Printing the tensor
x.print();

Output:

Tensor
   [1 + 0j, 2 + 0j, 3 + 0j]

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


Article Tags :