***Only for use by descendants of Metric***.
(self, name, shape=None, dtype=None, initializer=None)
| 258 | |
| 259 | # ---- For use by descendants --- |
| 260 | def add_variable(self, name, shape=None, dtype=None, initializer=None): |
| 261 | """***Only for use by descendants of Metric***.""" |
| 262 | if self._built: |
| 263 | raise RuntimeError("Can't call add_variable() except in build().") |
| 264 | if context.executing_eagerly(): |
| 265 | collections = None |
| 266 | else: |
| 267 | if self._use_global_variables: |
| 268 | collections = [ops.GraphKeys.GLOBAL_VARIABLES] |
| 269 | else: |
| 270 | collections = [ops.GraphKeys.LOCAL_VARIABLES] |
| 271 | collections += [ops.GraphKeys.METRIC_VARIABLES] |
| 272 | # Variables are Trackable dependencies of Metrics regardless of the |
| 273 | # global/local distinction. Users can avoid saving variables by not adding a |
| 274 | # dependency on the Metric. |
| 275 | v = self._add_variable_with_custom_getter( |
| 276 | name=name, |
| 277 | shape=shape, |
| 278 | dtype=dtype, |
| 279 | initializer=initializer, |
| 280 | trainable=False, |
| 281 | collections=collections, |
| 282 | use_resource=True, |
| 283 | getter=variable_scope.get_variable, |
| 284 | # Raise duplicate variable exceptions from get_variable rather than |
| 285 | # Trackable. |
| 286 | overwrite=True) |
| 287 | self._vars.append(v) |
| 288 | if context.executing_eagerly(): |
| 289 | self._initial_values[v] = v.value() |
| 290 | return v |
| 291 | |
| 292 | |
| 293 | class Mean(Metric): |
no test coverage detected