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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With