Type Metric
Namespace tensorflow.keras.metrics
Parent Layer
Interfaces IMetric
Encapsulates metric logic and state. Usage:
Usage with tf.keras API:
To be implemented by subclasses:
* `__init__()`: All state variables should be created in this method by
calling `self.add_weight()` like: `self.var = self.add_weight(...)`
* `update_state()`: Has all updates to the state variables like:
self.var.assign_add(...).
* `result()`: Computes and returns a value for the metric
from the state variables. Example subclass implementation: ```
class BinaryTruePositives(tf.keras.metrics.Metric): def __init__(self, name='binary_true_positives', **kwargs):
super(BinaryTruePositives, self).__init__(name=name, **kwargs)
self.true_positives = self.add_weight(name='tp', initializer='zeros') def update_state(self, y_true, y_pred, sample_weight=None):
y_true = tf.cast(y_true, tf.bool)
y_pred = tf.cast(y_pred, tf.bool) values = tf.logical_and(tf.equal(y_true, True), tf.equal(y_pred, True))
values = tf.cast(values, self.dtype)
if sample_weight is not None:
sample_weight = tf.cast(sample_weight, self.dtype)
sample_weight = tf.broadcast_weights(sample_weight, values)
values = tf.multiply(values, sample_weight)
self.true_positives.assign_add(tf.reduce_sum(values)) def result(self):
return self.true_positives
```
Show Example
m = SomeMetric(...) for input in...: m.update_state(input) print('Final result: ', m.result().numpy())
Methods
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight
- add_weight_dyn
- result
- result_dyn
- update_state
- update_state
- update_state_dyn
- update_state_dyn
Properties
- activity_regularizer
- activity_regularizer_dyn
- built
- dtype
- dtype_dyn
- dynamic
- dynamic_dyn
- inbound_nodes
- inbound_nodes_dyn
- input
- input_dyn
- input_mask
- input_mask_dyn
- input_shape
- input_shape_dyn
- input_spec
- input_spec_dyn
- losses
- losses_dyn
- metrics
- metrics_dyn
- name
- name_dyn
- name_scope
- name_scope_dyn
- non_trainable_variables
- non_trainable_variables_dyn
- non_trainable_weights
- non_trainable_weights_dyn
- outbound_nodes
- outbound_nodes_dyn
- output
- output_dyn
- output_mask
- output_mask_dyn
- output_shape
- output_shape_dyn
- PythonObject
- stateful
- submodules
- submodules_dyn
- supports_masking
- trainable
- trainable_dyn
- trainable_variables
- trainable_variables_dyn
- trainable_weights
- trainable_weights_dyn
- updates
- updates_dyn
- variables
- variables_dyn
- weights
- weights_dyn
Public instance methods
object add_weight(string name, Dimension shape, bool aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, Dimension shape, IDictionary<object, object> aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, int shape, VariableAggregation aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, Dimension shape, DType aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, Dimension shape, VariableAggregation aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, int shape, object aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, Dimension shape, object aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, int shape, DType aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, int shape, string aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, TensorShape shape, bool aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, TensorShape shape, IDictionary<object, object> aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, TensorShape shape, DType aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, TensorShape shape, VariableAggregation aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, TensorShape shape, object aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, TensorShape shape, string aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, Dimension shape, string aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, ImplicitContainer<T> shape, bool aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, ImplicitContainer<T> shape, IDictionary<object, object> aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, IEnumerable<Nullable<int>> shape, string aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, ImplicitContainer<T> shape, DType aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, int shape, IDictionary<object, object> aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, ImplicitContainer<T> shape, VariableAggregation aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, IEnumerable<Nullable<int>> shape, IDictionary<object, object> aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, ImplicitContainer<T> shape, object aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, IEnumerable<Nullable<int>> shape, bool aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, ImplicitContainer<T> shape, string aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, IEnumerable<Nullable<int>> shape, VariableAggregation aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, int shape, bool aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, IEnumerable<Nullable<int>> shape, object aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight(string name, IEnumerable<Nullable<int>> shape, DType aggregation, ImplicitContainer<T> synchronization, object initializer, DType dtype)
Adds state variable. Only for use by subclasses.
object add_weight_dyn(object name, ImplicitContainer<T> shape, ImplicitContainer<T> aggregation, ImplicitContainer<T> synchronization, object initializer, object dtype)
Adds state variable. Only for use by subclasses.
object result()
Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the
metric value using the state variables.
object result_dyn()
Computes and returns the metric value tensor. Result computation is an idempotent operation that simply calculates the
metric value using the state variables.
object update_state(IGraphNodeBase[] args)
Accumulates statistics for computing the element-wise mean.
Returns
-
object
- Update op.
object update_state(IDictionary<string, object> kwargs, IGraphNodeBase[] args)
Accumulates statistics for computing the element-wise mean.
Returns
-
object
- Update op.
object update_state_dyn(Object[] args)
Accumulates statistics for computing the element-wise mean.
Returns
-
object
- Update op.
object update_state_dyn(IDictionary<string, object> kwargs, Object[] args)
Accumulates statistics for computing the element-wise mean.
Returns
-
object
- Update op.