What's the difference between Variable and ResourceVariable in Tensorflow

Issue

In Tensorflow, Variable is a resource, inherited from ResourceBase and managed by ResourceMgr. But why is there another named ResourceVariable? Both of them can be used for optimizers like gradient_descent (see this example). What’s the difference? I know the former is well documented and most often used. What’s the purpose of the latter?

Solution

ResourceVariable is the replacement for Variable, that aims to clean up some of the messier aspects of the semantics of Variable.

ResourceVariable is the default in TF 2.0 and you very likely don’t care about the differences between the two unless you are working on details deep inside the Tensorflow implementation. When eager execution is enabled tf.Variable also creates resource variables.

So just use tf.Variable for now, it’s almost certainly what you want; if you experience issues which look like race conditions or bugs from inconsistent values of variables in code you can try enabling resource variables (by either passing use_resource=True to your variable-creating code or calling tf.enable_resource_variables() in TF 1.x).

Answered By – Peter Hawkins

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published