When developing a model, it is useful to quantify its performance with a pre-defined metric. For classification problem for example, the use of log loss is common, and for regression problem the mean squared error is typically used. Keras integrates those 2 metrics per default.

However, we might come across problems where we might need to come up with our own metrics. In Keras, it is possible to define custom metrics, as well as custom loss functions.

In this post, I will show you: how to create a function that calculates the coefficient of determination R2, and how to call the function when compiling the model in Keras.

# Definition of the Coefficient of Determination R2

The coefficient of determination R2 can describe how “good” a model is at making predictions: it represents the proportion of the variance in the dependent variable that is predictable from the independent variable:

where:

1. $SS_{tot} = \sum(y_i - \bar{y})^2$ is the total sum of squares, with $\bar{y} = 1/n \sum y_i$,

2. $SS_{res} = \sum(y_i - \hat{y}_i)^2$ is the residual sum of squares, and $\hat{y}_i$ is the predicted value.

R2 ranges from 0 to 1:

1. if R2=0 : the model always fails to predict the target variable,
2. if R2=1 : the model perfectly predicts the target variable.

Any value between 0 and 1 indicates what percentage of the target variable, using the model, can be explained by the features. If R2< 0, it indicates that the model is no better than one that constantly predicts the mean of the target variable.

# Implementation

OK, now that we’ve got that out of the way, let’s write the code!

To avoid division by zero, I use K.epsilon() which has the value 1E-8.

Pretty simple, right? Hope you found this post useful!!!