Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Variable and get_variable in TensorFlow

As far as I know, Variable is the default operation for making a variable, and get_variable is mainly used for weight sharing.

On the one hand, there are some people suggesting using get_variable instead of the primitive Variable operation whenever you need a variable. On the other hand, I merely see any use of get_variable in TensorFlow's official documents and demos.

Thus I want to know some rules of thumb on how to correctly use these two mechanisms. Are there any "standard" principles?

like image 228
Lifu Huang Avatar asked May 08 '16 09:05

Lifu Huang


People also ask

What is the difference between tensor and variable TensorFlow?

Tensors v.s. VariablesA variable in Tensorflow is also a wrapper around a tensor, but has a different meaning. A variable contains a tensor that is persistent and changeable across different Session.

What does TF Get_variable do?

The function tf. get_variable() returns the existing variable with the same name if it exists, and creates the variable with the specified shape and initializer if it does not exist.


1 Answers

I'd recommend to always use tf.get_variable(...) -- it will make it way easier to refactor your code if you need to share variables at any time, e.g. in a multi-gpu setting (see the multi-gpu CIFAR example). There is no downside to it.

Pure tf.Variable is lower-level; at some point tf.get_variable() did not exist so some code still uses the low-level way.

like image 126
Lukasz Kaiser Avatar answered Oct 13 '22 19:10

Lukasz Kaiser