Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are metrics computed in Keras?

I am wondering how does Keras compute a metric (a custom one or not).

For example, suppose I have the following metric which yields the maximal error between the prediction and the ground truth:

def max_error(y_true, y_pred):

    import keras.backend as K

    return K.max(K.abs(y_true-y_pred))

Is the output scalar metric computed on all mini-batches and then averaged or is the metric directly computed on the whole dataset (training or validation)?

like image 860
floflo29 Avatar asked Mar 19 '18 09:03

floflo29


People also ask

What are the metrics in Keras?

A metric is a function that is used to judge the performance of your model. Metric functions are similar to loss functions, except that the results from evaluating a metric are not used when training the model. Note that you may use any loss function as a metric.

How the accuracy is computed in Keras?

Accuracy calculates the percentage of predicted values (yPred) that match with actual values (yTrue). For a record, if the predicted value is equal to the actual value, it is considered accurate. We then calculate Accuracy by dividing the number of accurately predicted records by the total number of records.


1 Answers

Something additional to know with respect to the metric for the VALIDATION set:

Contrary to what is suggested in another answer, I just saw that the metric on the validation set is calculated in batches, and then averaged (of course the trained model at the end of the epoch is used, in contrast to how the metric score is calculated for the training set).

If you want to compute it on the whole validation data at once, you have to use a callback as described in the accepted answer of guangshengzuo (see https://keras.io/guides/writing_your_own_callbacks/ for more details).

Sure, for the usual metrics, there will not be any difference whether you calculate first in batches and average, or do it all in one big batch. BUT for custom metrics, there very well can be: I just had a case where the metric would tune a parameter, based on the data.

Edit: added link on callbacks, in response to comment

like image 200
tyrex Avatar answered Oct 23 '22 21:10

tyrex