Instantiates a variable and returns it.
(value, dtype=None, name=None, constraint=None)
| 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 |
nothing calls this directly
no test coverage detected