# tf.contrib.eager.metrics.SparseAccuracy

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/eager/python/metrics_impl.py#L502-L547) |

Calculates how often `predictions` matches `labels`.

Inherits From: [`Mean`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/contrib/eager/metrics/Mean)

    tf.contrib.eager.metrics.SparseAccuracy(
        name=None, dtype=tf.dtypes.double
    )

This class is compatible with
[`tf.keras.losses.sparse_categorical_crossentropy`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/keras/losses/sparse_categorical_crossentropy),
[`tf.nn.sparse_softmax_cross_entropy_with_logits`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/nn/sparse_softmax_cross_entropy_with_logits),
[`tf.compat.v1.losses.sparse_softmax_cross_entropy`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/losses/sparse_softmax_cross_entropy).

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `name` | name of the accuracy object |
| `dtype` | data type of tensor. |
| `variables` | <br /> <br /> |

<br />

## Methods

### `add_variable`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/eager/python/metrics_impl.py#L260-L290)

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

***Only for use by descendants of Metric***.

### `aggregate`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/eager/python/metrics_impl.py#L237-L256)

    aggregate(
        metrics
    )

Adds in the state from a list of metrics.

Default implementation sums all the metric variables.

<br />

<br />

<br />

| Args ||
|---|---|
| `metrics` | A list of metrics with the same type as `self`. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If metrics contains invalid data. |

<br />

### `build`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/eager/python/metrics_impl.py#L302-L311)

    build(
        *args, **kwargs
    )

Method to create variables.

Called by `__call__()` before `call()` for the first time.

<br />

<br />

<br />

| Args ||
|---|---|
| `*args` | <br /> <br /> |
| `**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. |

<br />

### `call`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/eager/python/metrics_impl.py#L520-L547)

    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.

<br />

<br />

<br />

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

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The arguments, for easy chaining. ||

<br />

### `init_variables`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/eager/python/metrics_impl.py#L160-L177)

    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.)

<br />

<br />

<br />

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

<br />

### `result`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/eager/python/metrics_impl.py#L340-L362)

    result(
        write_summary=True
    )

Returns the result of the Metric.

<br />

<br />

<br />

| Args ||
|---|---|
| `write_summary` | bool indicating whether to feed the result to the summary before returning. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| aggregated metric as float. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | if the optional argument is not bool |

<br />

### `value`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/eager/python/metrics_impl.py#L217-L222)

    value()

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

### `__call__`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/eager/python/metrics_impl.py#L135-L150)

    __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.

<br />

<br />

<br />

| Args ||
|---|---|
| `*args` | <br /> <br /> |
| `**kwargs` | A mini-batch of inputs to the Metric, passed on to `call()`. |

<br />