Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you compute accuracy in a regression model, after rounding predictions to classes, in keras?

How would you create and display an accuracy metric in keras for a regression problem, for example after you round the predictions to the nearest integer class?

While accuracy is not itself effectively defined conventionally for a regression problem, to determine ordinal classes/labels for data, it is suitable to treat the problem as a regression. But then it would be convenient to also calculate an accuracy metric, whether it be kappa or something else like that. Here is a basic keras boilerplate code to modify.

from keras.models import Sequential
from keras.layers.core import Dense, Activation

model = Sequential()
model.add(Dense(10, 64))
model.add(Activation('tanh'))
model.add(Dense(64, 1))
model.compile(loss='mean_absolute_error', optimizer='rmsprop')

model.fit(X_train, y_train, nb_epoch=20, batch_size=16)
score = model.evaluate(X_test, y_test, batch_size=16)
like image 582
mikal94305 Avatar asked Mar 08 '17 07:03

mikal94305


People also ask

How do you find the prediction accuracy in a regression model?

You can evaluate the accuracy of a regression model (including nonlinear ones) by comparing the predicted values to the actual values. I'd say the best way to do this is graphically with e.g. boxplots of the residuals, a scatterplot of actual vs.

How is accuracy calculated 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

I use rounded accuracy like this:

from keras import backend as K

def soft_acc(y_true, y_pred):
    return K.mean(K.equal(K.round(y_true), K.round(y_pred)))

model.compile(..., metrics=[soft_acc])
like image 169
Thomas Pinetz Avatar answered Oct 06 '22 13:10

Thomas Pinetz