TensorFlow 2.0: how to group graph using tf.keras? tf.name_scope/tf.variable_scope not used anymore? TensorFlow 2.0: how to group graph using tf.keras? tf.name_scope/tf.variable_scope not used anymore? python python

TensorFlow 2.0: how to group graph using tf.keras? tf.name_scope/tf.variable_scope not used anymore?


According to the community RFC Variables in TensorFlow 2.0:

  • to control variable naming users can use tf.name_scope + tf.Variable

Indeed, tf.name_scope still exists in TensorFlow 2.0, so you can just do:

with tf.name_scope("foo"):    with tf.name_scope("bar"):        v = tf.Variable([0], dtype=tf.float32, name="v")        assert v.name == "foo/bar/v:0"

Also, as the point above that states:

  • the tf 1.0 version of variable_scope and get_variable will be left in tf.compat.v1

So you can just fall back to tf.compat.v1.variable_scope and tf.compat.v1.get_variable if you really need to.

Variable scopes and tf.get_variable can be convenient but are riddled with minor pitfalls and corner cases, specially since they behave similarly but not exactly like name scopes, and it is actually a parallel mechanism to it. I think having just name scopes will be more consistent and straightforward.