![]() |
Class Metric
A metric holds state for aggregating statistics over an evaluation run.
Inherits From: CheckpointableBase
Example use with eager execution:
m = SomeMetric(...)
for input in ...:
m(input)
print(m.result())
Example use with graph execution:
m = SomeMetric(...)
inputs = ... # Some tensors to compute the metric on.
m_update = m(inputs)
# Variables defined in first call, so get the initialization op afterwards.
m_init = m.init_variables() # or tf.compat.v1.global_variables_initializer()
m_result = m.result()
with tf.compat.v1.Session() as sess:
sess.run(m_init)
for input in ...:
sess.run(m_update)
print(sess.run(m_result))
Example use with graph execution with placeholders and feed_dict:
m = SomeMetric(...)
m_placeholder = tf.compat.v1.placeholder(...)
m_update = m(m_placeholder)
# Variables defined in first call, so get the initialization op afterwards.
m_init = m.init_variables() # or tf.compat.v1.global_variables_initializer()
m_result = m.result()
with tf.compat.v1.Session() as sess:
sess.run(m_init)
for input in ...:
sess.run(m_update, feed_dict={m_placeholder: input})
print(sess.run(m_result))
Descendants will implement:
* build()
: All variables should be created in this method, by calling
self.add_variable()
as in: self.var = self.add_variable(...)
build() will be called in the first invocation of __call__()
, with
the same arguments passed call()
.
* call()
: Has all updates to variables, as in:
self.var.assign_add(...)
* result()
: Computes and returns a final value for the metric
from the variables in self
.
Descendants may override aggregate()
, but usually won't need to. It
adds in the state from a list of metrics of the same type as self
.
(Default is to sum all the variables.) Note that users should not call
aggregate()
, it is for use by TensorFlow infrastructure.
__init__
__init__(
name=None,
use_global_variables=False
)
Initialize self. See help(type(self)) for accurate signature.
Properties
name
variables
Methods
tf.contrib.eager.metrics.Metric.__call__
__call__(
*args,
**kwargs
)
Returns op to execute to update this metric for these inputs.
Returns None if eager execution is enabled. Returns a graph-mode function if graph execution is enabled.
Args:
*args
: ***kwargs
: A mini-batch of inputs to the Metric, passed on tocall()
.
tf.contrib.eager.metrics.Metric.add_variable
add_variable(
name,
shape=None,
dtype=None,
initializer=None
)
Only for use by descendants of Metric.
tf.contrib.eager.metrics.Metric.aggregate
aggregate(metrics)
Adds in the state from a list of metrics.
Default implementation sums all the metric variables.
Args:
metrics
: A list of metrics with the same type asself
.
Raises:
ValueError
: If metrics contains invalid data.
tf.contrib.eager.metrics.Metric.build
build(
*args,
**kwargs
)
Method to create variables.
Called by __call__()
before call()
for the first time.
Args:
*args
: ***kwargs
: The arguments to the first invocation of__call__()
.build()
may use the shape and/or dtype of these arguments when deciding how to create variables.
tf.contrib.eager.metrics.Metric.call
call(
*args,
**kwargs
)
Accumulates statistics for the metric. Users should use call instead.
Args:
*args
: ***kwargs
: A mini-batch of inputs to the Metric, as passed to__call__()
.
tf.contrib.eager.metrics.Metric.init_variables
init_variables()
Initializes this Metric's variables.
Should be called after variables are created in the first execution
of __call__()
. If using graph execution, the return value should be
run()
in a session before running the op returned by __call__()
.
(See example above.)
Returns:
If using graph execution, this returns an op to perform the initialization. Under eager execution, the variables are reset to their initial values as a side effect and this function returns None.
tf.contrib.eager.metrics.Metric.result
result()
Computes and returns a final value for the metric.
tf.contrib.eager.metrics.Metric.value
value()
In graph mode returns the result Tensor while in eager the callable.