TF 2.0 is out! Get hands-on practice at TF World, Oct 28-31. Use code TF20 for 20% off select passes. Register now

tf.contrib.eager.metrics.SparseAccuracy

View source on GitHub

Class SparseAccuracy

Calculates how often predictions matches labels.

Inherits From: Mean

This class is compatible with tf.keras.losses.sparse_categorical_crossentropy, tf.nn.sparse_softmax_cross_entropy_with_logits, tf.compat.v1.losses.sparse_softmax_cross_entropy.

Attributes:

  • name: name of the accuracy object
  • dtype: data type of tensor.

__init__

View source

__init__(
    name=None,
    dtype=tf.dtypes.double
)

Inits SparseAccuracy with name and dtype.

Properties

name

variables

Methods

__call__

View source

__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 to call().

add_variable

View source

add_variable(
    name,
    shape=None,
    dtype=None,
    initializer=None
)

Only for use by descendants of Metric.

aggregate

View source

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 as self.

Raises:

  • ValueError: If metrics contains invalid data.

build

View source

build(
    *args,
    **kwargs
)

call

View source

call(
    labels,
    predictions,
    weights=None
)

Accumulate accuracy statistics.

labels and predictions should have the same shape except the predictions must have one additional trailing dimension equal to the number of classes(you want to predict).

Type of labels and predictions can be different.

Args:

  • labels: Tensor of shape (batch_size, ) containing integers
  • predictions: Tensor with the logits or probabilities for each example.
  • weights: Optional weighting of each example. Defaults to 1.

Returns:

The arguments, for easy chaining.

init_variables

View source

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.

result

View source

result(write_summary=True)

Returns the result of the Metric.

Args:

  • write_summary: bool indicating whether to feed the result to the summary before returning.

Returns:

aggregated metric as float.

Raises:

  • ValueError: if the optional argument is not bool

value

View source

value()

In graph mode returns the result Tensor while in eager the callable.