Wednesday, July 3, 2024
HomeLanguagesPythonDifference between Variable and get_variable in TensorFlow

Difference between Variable and get_variable in TensorFlow

In this article, we will try to understand the difference between the Variable() and get_variable() function available in the TensorFlow Framework.

Variable() Method in TensorFlow

A variable maintains a shared, persistent state manipulated by a program. If one uses this function then it will create a new variable. Tensorflow version 2.0 does support variable().

Python3




import tensorflow as tf
v = tf.Variable(1.)
v.assign(2.)
print(v)


Output:

<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=2.0>

get_variable() Method in TensorFlow

The get_variable() function creates a new variable called a name(whichever you specify) or adds the existing name of the current scope in the TensorFlow graph. It can be used to create new or add Existing variables. It makes it easier to refactor your code.

Python3




import tensorflow as tf
s = tf.compat.v1.get_variable(name='tens',
                              shape=[1],
                              dtype=tf.int32)
print(s)


Output:

<tf.Variable 'tens:0' shape=(1,) dtype=int32, numpy=array([0], dtype=int32)>

There are times when you are working with GPUs and multiple processors then you would like to use variable sharing as a feature so, let’s say you are trying to optimize the weights of a neural network then all the processors must optimize the same parameter parallelly then only we will be able to speed up the training and the optimization process.

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments