1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
## Usage of metrics
A metric is a function that is used to judge the performance of your model. Metric functions are to be supplied in the `metrics` parameter when a model is compiled.
```python
model.compile(loss='mean_squared_error',
optimizer='sgd',
metrics=['mae', 'acc'])
```
```python
from keras import metrics
model.compile(loss='mean_squared_error',
optimizer='sgd',
metrics=[metrics.mae, metrics.categorical_accuracy])
```
A metric function is similar to a [loss function](/losses), except that the results from evaluating a metric are not used when training the model. You may use any of the loss functions as a metric function.
You can either pass the name of an existing metric, or pass a Theano/TensorFlow symbolic function (see [Custom metrics](#custom-metrics)).
#### Arguments
- __y_true__: True labels. Theano/TensorFlow tensor.
- __y_pred__: Predictions. Theano/TensorFlow tensor of the same shape as y_true.
#### Returns
Single tensor value representing the mean of the output array across all
datapoints.
----
## Available metrics
### accuracy
```python
keras.metrics.accuracy(y_true, y_pred)
```
----
### binary_accuracy
```python
keras.metrics.binary_accuracy(y_true, y_pred, threshold=0.5)
```
----
### categorical_accuracy
```python
keras.metrics.categorical_accuracy(y_true, y_pred)
```
----
### sparse_categorical_accuracy
```python
keras.metrics.sparse_categorical_accuracy(y_true, y_pred)
```
----
### top_k_categorical_accuracy
```python
keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=5)
```
----
### sparse_top_k_categorical_accuracy
```python
keras.metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=5)
```
----
### cosine_proximity
```python
keras.metrics.cosine_proximity(y_true, y_pred, axis=-1)
```
----
### clone_metric
```python
keras.metrics.clone_metric(metric)
```
Returns a clone of the metric if stateful, otherwise returns it as is.
----
### clone_metrics
```python
keras.metrics.clone_metrics(metrics)
```
Clones the given metric list/dict.
In addition to the metrics above, you may use any of the loss functions described in the [loss function](/losses) page as metrics.
----
## Custom metrics
Custom metrics can be passed at the compilation step. The
function would need to take `(y_true, y_pred)` as arguments and return
a single tensor value.
```python
import keras.backend as K
def mean_pred(y_true, y_pred):
return K.mean(y_pred)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy', mean_pred])
```
|