Open In App

Difference between Variable and get_variable in TensorFlow

Last Updated : 09 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads