Python tensorflow.python.ops.variable_scope._pure_variable_scope() Examples
The following are 7
code examples of tensorflow.python.ops.variable_scope._pure_variable_scope().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.python.ops.variable_scope
, or try the search function
.
Example #1
Source File: linear.py From estimator with Apache License 2.0 | 5 votes |
def build(self, _): # We need variable scopes for now because we want the variable partitioning # information to percolate down. We also use _pure_variable_scope's here # since we want to open up a name_scope in the `call` method while creating # the ops. with variable_scope._pure_variable_scope(self.name): # pylint: disable=protected-access for column in self._feature_columns: with variable_scope._pure_variable_scope( # pylint: disable=protected-access fc_v2._sanitize_column_name_for_variable_scope(column.name)): # pylint: disable=protected-access # Create the state for each feature column column.create_state(self._state_manager) # Create a weight variable for each column. if isinstance(column, fc_v2.CategoricalColumn): first_dim = column.num_buckets else: first_dim = column.variable_shape.num_elements() self._state_manager.create_variable( column, name='weights', dtype=tf.float32, shape=(first_dim, self._units), initializer=tf.keras.initializers.zeros(), trainable=self.trainable) # Create a bias variable. self.bias = self.add_variable( name='bias_weights', dtype=tf.float32, shape=[self._units], initializer=tf.keras.initializers.zeros(), trainable=self.trainable, use_resource=True, # TODO(rohanj): Get rid of this hack once we have a mechanism for # specifying a default partitioner for an entire layer. In that case, # the default getter for Layers should work. getter=variable_scope.get_variable) super(_LinearModelLayer, self).build(None)
Example #2
Source File: template.py From lambda-packs with MIT License | 4 votes |
def __init__(self, name, func, create_scope_now=False, unique_name=None, custom_getter=None): """Creates a template for the given function. Args: name: A name for the scope created by this template. The name will be made unique by appending `_N` to the it (see how `tf.variable_scope` treats the `default_name` for details). func: The function to apply each time. create_scope_now: Whether to create the scope at Template construction time, rather than first call. Defaults to false. Creating the scope at construction time may be more convenient if the template is to passed through much lower level code, and you want to be sure of the scope name without knowing exactly where it will be first called. If set to True, the scope will be created in the constructor, and all subsequent times in __call__, leading to a trailing numeral being added to the names of all created Tensors. If set to False, the scope will be created at the first call location. unique_name: When used, it overrides name_ and is not made unique. If a template of the same scope/unique_name already exists and reuse is false, an error is raised. Defaults to None. custom_getter: optional custom getter to pass to variable_scope() Raises: ValueError: if the name is None. """ self._func = func self._stacktrace = traceback.format_stack()[:-2] self._name = name self._unique_name = unique_name self._custom_getter = custom_getter if name is None: raise ValueError("name cannot be None.") if create_scope_now: with variable_scope._pure_variable_scope( # pylint:disable=protected-access (self._unique_name or variable_scope._get_unique_variable_scope(self._name)), # pylint:disable=protected-access custom_getter=self._custom_getter) as vs: self._variable_scope = vs else: self._variable_scope = None # This variable keeps track of whether the template has been called yet, # which is not the same as whether the scope has been created. self._variables_created = False
Example #3
Source File: utils.py From Gun-Detector with Apache License 2.0 | 4 votes |
def create_variables_in_class_scope(method): """Force the variables constructed in this class to live in the sonnet module. Wraps a method on a sonnet module. For example the following will create two different variables. ``` class Mod(snt.AbstractModule): @create_variables_in_class_scope def dynamic_thing(self, input, name): return snt.Linear(name)(input) mod.dynamic_thing(x, name="module_nameA") mod.dynamic_thing(x, name="module_nameB") # reuse mod.dynamic_thing(y, name="module_nameA") ``` """ @functools.wraps(method) def wrapper(obj, *args, **kwargs): def default_context_manager(reuse=None): variable_scope = obj.variable_scope return tf.variable_scope(variable_scope, reuse=reuse) variable_scope_context_manager = getattr(obj, "_enter_variable_scope", default_context_manager) graph = tf.get_default_graph() # Temporarily enter the variable scope to capture it with variable_scope_context_manager() as tmp_variable_scope: variable_scope = tmp_variable_scope with variable_scope_ops._pure_variable_scope( variable_scope, reuse=tf.AUTO_REUSE) as pure_variable_scope: name_scope = variable_scope.original_name_scope if name_scope[-1] != "/": name_scope += "/" with tf.name_scope(name_scope): sub_scope = snt_util.to_snake_case(method.__name__) with tf.name_scope(sub_scope) as scope: out_ops = method(obj, *args, **kwargs) return out_ops return wrapper
Example #4
Source File: utils.py From g-tensorflow-models with Apache License 2.0 | 4 votes |
def create_variables_in_class_scope(method): """Force the variables constructed in this class to live in the sonnet module. Wraps a method on a sonnet module. For example the following will create two different variables. ``` class Mod(snt.AbstractModule): @create_variables_in_class_scope def dynamic_thing(self, input, name): return snt.Linear(name)(input) mod.dynamic_thing(x, name="module_nameA") mod.dynamic_thing(x, name="module_nameB") # reuse mod.dynamic_thing(y, name="module_nameA") ``` """ @functools.wraps(method) def wrapper(obj, *args, **kwargs): def default_context_manager(reuse=None): variable_scope = obj.variable_scope return tf.variable_scope(variable_scope, reuse=reuse) variable_scope_context_manager = getattr(obj, "_enter_variable_scope", default_context_manager) graph = tf.get_default_graph() # Temporarily enter the variable scope to capture it with variable_scope_context_manager() as tmp_variable_scope: variable_scope = tmp_variable_scope with variable_scope_ops._pure_variable_scope( variable_scope, reuse=tf.AUTO_REUSE) as pure_variable_scope: name_scope = variable_scope.original_name_scope if name_scope[-1] != "/": name_scope += "/" with tf.name_scope(name_scope): sub_scope = snt_util.to_snake_case(method.__name__) with tf.name_scope(sub_scope) as scope: out_ops = method(obj, *args, **kwargs) return out_ops return wrapper
Example #5
Source File: utils.py From models with Apache License 2.0 | 4 votes |
def create_variables_in_class_scope(method): """Force the variables constructed in this class to live in the sonnet module. Wraps a method on a sonnet module. For example the following will create two different variables. ``` class Mod(snt.AbstractModule): @create_variables_in_class_scope def dynamic_thing(self, input, name): return snt.Linear(name)(input) mod.dynamic_thing(x, name="module_nameA") mod.dynamic_thing(x, name="module_nameB") # reuse mod.dynamic_thing(y, name="module_nameA") ``` """ @functools.wraps(method) def wrapper(obj, *args, **kwargs): def default_context_manager(reuse=None): variable_scope = obj.variable_scope return tf.variable_scope(variable_scope, reuse=reuse) variable_scope_context_manager = getattr(obj, "_enter_variable_scope", default_context_manager) graph = tf.get_default_graph() # Temporarily enter the variable scope to capture it with variable_scope_context_manager() as tmp_variable_scope: variable_scope = tmp_variable_scope with variable_scope_ops._pure_variable_scope( variable_scope, reuse=tf.AUTO_REUSE) as pure_variable_scope: name_scope = variable_scope.original_name_scope if name_scope[-1] != "/": name_scope += "/" with tf.name_scope(name_scope): sub_scope = snt_util.to_snake_case(method.__name__) with tf.name_scope(sub_scope) as scope: out_ops = method(obj, *args, **kwargs) return out_ops return wrapper
Example #6
Source File: template.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 4 votes |
def __init__(self, name, func, create_scope_now=False, unique_name=None, custom_getter=None): """Creates a template for the given function. Args: name: A name for the scope created by this template. The name will be made unique by appending `_N` to the it (see how `tf.variable_scope` treats the `default_name` for details). func: The function to apply each time. create_scope_now: Whether to create the scope at Template construction time, rather than first call. Defaults to false. Creating the scope at construction time may be more convenient if the template is to passed through much lower level code, and you want to be sure of the scope name without knowing exactly where it will be first called. If set to True, the scope will be created in the constructor, and all subsequent times in __call__, leading to a trailing numeral being added to the names of all created Tensors. If set to False, the scope will be created at the first call location. unique_name: When used, it overrides name_ and is not made unique. If a template of the same scope/unique_name already exists and reuse is false, an error is raised. Defaults to None. custom_getter: optional custom getter to pass to variable_scope() Raises: ValueError: if the name is None. """ self._func = func self._stacktrace = traceback.format_stack()[:-2] self._name = name self._unique_name = unique_name self._custom_getter = custom_getter if name is None: raise ValueError("name cannot be None.") if create_scope_now: with variable_scope._pure_variable_scope( # pylint:disable=protected-access (self._unique_name or variable_scope._get_unique_variable_scope(self._name)), # pylint:disable=protected-access custom_getter=self._custom_getter) as vs: self._variable_scope = vs else: self._variable_scope = None # This variable keeps track of whether the template has been called yet, # which is not the same as whether the scope has been created. self._variables_created = False
Example #7
Source File: utils.py From multilabel-image-classification-tensorflow with MIT License | 4 votes |
def create_variables_in_class_scope(method): """Force the variables constructed in this class to live in the sonnet module. Wraps a method on a sonnet module. For example the following will create two different variables. ``` class Mod(snt.AbstractModule): @create_variables_in_class_scope def dynamic_thing(self, input, name): return snt.Linear(name)(input) mod.dynamic_thing(x, name="module_nameA") mod.dynamic_thing(x, name="module_nameB") # reuse mod.dynamic_thing(y, name="module_nameA") ``` """ @functools.wraps(method) def wrapper(obj, *args, **kwargs): def default_context_manager(reuse=None): variable_scope = obj.variable_scope return tf.variable_scope(variable_scope, reuse=reuse) variable_scope_context_manager = getattr(obj, "_enter_variable_scope", default_context_manager) graph = tf.get_default_graph() # Temporarily enter the variable scope to capture it with variable_scope_context_manager() as tmp_variable_scope: variable_scope = tmp_variable_scope with variable_scope_ops._pure_variable_scope( variable_scope, reuse=tf.AUTO_REUSE) as pure_variable_scope: name_scope = variable_scope.original_name_scope if name_scope[-1] != "/": name_scope += "/" with tf.name_scope(name_scope): sub_scope = snt_util.to_snake_case(method.__name__) with tf.name_scope(sub_scope) as scope: out_ops = method(obj, *args, **kwargs) return out_ops return wrapper