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

Function opt_variable

tensorflow/contrib/tpu/python/tpu/keras_tpu_variables.py:311–340  ·  view source on GitHub ↗

Instantiates a variable and returns it.

(value, dtype=None, name=None, constraint=None)

Source from the content-addressed store, hash-verified

309 old_v = backend.variable
310
311 def opt_variable(value, dtype=None, name=None, constraint=None):
312 """Instantiates a variable and returns it."""
313 if dtype is None:
314 dtype = backend.floatx()
315
316 variables = []
317 for i in range(num_replicas):
318 # Keras holds the variables in optimizer class instance , so the name
319 # does not matter here. ResourceVariable constructor will find a unique
320 # name (including name=None) for each replica.
321 with ops.device("device:TPU:{}".format(i)):
322 v = resource_variable_ops.ResourceVariable(
323 value,
324 dtype=dtypes_module.as_dtype(dtype),
325 name=name,
326 constraint=constraint)
327 variables.append(v)
328 name = "replicate_{}_{}".format("variable" if name is None else name,
329 ops.uid())
330 v = ReplicatedVariable(name, variables)
331
332 # pylint: disable=protected-access
333
334 if isinstance(value, np.ndarray):
335 v._keras_shape = value.shape
336 elif hasattr(value, "shape"):
337 v._keras_shape = backend.int_shape(value)
338 v._uses_learning_phase = False
339 backend.track_variable(v)
340 return v
341
342 backend.variable = opt_variable
343 yield

Callers

nothing calls this directly

Calls 7

ReplicatedVariableClass · 0.85
uidMethod · 0.80
rangeFunction · 0.50
deviceMethod · 0.45
formatMethod · 0.45
ResourceVariableMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected