Tensorflow.js tf.variable() Function Last Updated : 07 Jul, 2021 Comments Improve Suggest changes 1 Likes Like Report 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: initialValue: The initial value of the tensor from which the new variable will be initialized.trainable: It is an optional parameter. It is of boolean type if true optimizers are allowed to update the variable and if false optimizers are not allowed to update it.name: It is also an optional parameter. It is of string type. It is used for the name of the variable used as a unique ID.dtype: It is also an optional parameter. If it is passed as the argument the intialValue will be changed to the specified dtype. Return Value: This function returns tf.variable. Example 1: JavaScript // 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: JavaScript // 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: JavaScript // 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 Create Quiz Comment C CoderSaty Follow 1 Improve C CoderSaty Follow 1 Improve Article Tags : JavaScript Web Technologies Tensorflow.js Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like