VarScopeObject

class tfsnippet.VarScopeObject(name=None, scope=None)

Bases: object

Base class for objects that own a variable scope.

The VarScopeObject can be used along with instance_reuse(), for example:

class YourVarScopeObject(VarScopeObject):

    @instance_reuse
    def foo(self):
        return tf.get_variable('bar', ...)

o = YourVarScopeObject('object_name')
o.foo()  # You should get a variable with name "object_name/foo/bar"

To build variables in the constructor of derived classes, you may use reopen_variable_scope(self.variable_scope) to open the original variable scope and its name scope, right after the constructor of VarScopeObject has been called, for example:

class YourVarScopeObject(VarScopeObject):

    def __init__(self, name=None, scope=None):
        super(YourVarScopeObject, self).__init__(name=name, scope=scope)
        with reopen_variable_scope(self.variable_scope):
            self.w = tf.get_variable('w', ...)

Attributes Summary

name Get the name of this object.
variable_scope Get the variable scope of this object.

Attributes Documentation

name

Get the name of this object.

variable_scope

Get the variable scope of this object.