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

梦想与她 提交于 2020-02-26 23:08:56

问题


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)

回答1:


I use rounded accuracy like this:

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



回答2:


The answer by Thomas pretty much sums up the question. Just a minor addition as I was stuck in this one. Here "K" is

import keras.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])


来源:https://stackoverflow.com/questions/42665359/how-do-you-compute-accuracy-in-a-regression-model-after-rounding-predictions-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!