MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / VariableHolder

Class VariableHolder

tensorflow/python/eager/wrap_function.py:46–94  ·  view source on GitHub ↗

Holds variables for a python function.

Source from the content-addressed store, hash-verified

44
45
46class VariableHolder(object):
47 """Holds variables for a python function."""
48
49 def __init__(self, fn=None, share_variables=False):
50 self._fn = fn
51
52 self._share_variables = share_variables
53 self._variables_by_name = data_structures.Mapping()
54
55 @property
56 def variables(self):
57 return self._variables_by_name
58
59 def variable_creator_scope(self, next_creator, **kwargs):
60 """Creates variables & adds them to collections to match legacy code."""
61 collections = kwargs.pop("collections", None)
62 v = None
63
64 # Get expected variable name.
65 with ops.name_scope(kwargs.get("name", None), "Variable") as name:
66 variable_name = ops.name_from_scope_name(name)
67 kwargs["name"] = name
68
69 if self._share_variables:
70 v = self._variables_by_name.get(variable_name, None)
71
72 if v is None:
73 v = next_creator(**kwargs)
74 self._variables_by_name[variable_name] = v
75
76 if collections is None:
77 collections = [ops.GraphKeys.GLOBAL_VARIABLES]
78 if v.trainable and ops.GraphKeys.TRAINABLE_VARIABLES not in collections:
79 collections = list(collections) + [ops.GraphKeys.TRAINABLE_VARIABLES]
80
81 ops.add_to_collections(collections, v)
82
83 return v
84
85 def __call__(self, *args, **kwargs):
86 return self.call_with_variable_creator_scope(self._fn)(*args, **kwargs)
87
88 def call_with_variable_creator_scope(self, fn):
89
90 def wrapped(*args, **kwargs):
91 with variable_scope.variable_creator_scope(self.variable_creator_scope):
92 return fn(*args, **kwargs)
93
94 return wrapped
95
96
97def _get_element_from_tensor_info(tensor_info, graph):

Callers 2

__init__Method · 0.85
wrap_functionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected