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.
JavaScript
x
12
12
1
from keras.models import Sequential
2
from keras.layers.core import Dense, Activation
3
4
model = Sequential()
5
model.add(Dense(10, 64))
6
model.add(Activation('tanh'))
7
model.add(Dense(64, 1))
8
model.compile(loss='mean_absolute_error', optimizer='rmsprop')
9
10
model.fit(X_train, y_train, nb_epoch=20, batch_size=16)
11
score = model.evaluate(X_test, y_test, batch_size=16)
12
Advertisement
Answer
I use rounded accuracy like this:
JavaScript
1
7
1
from keras import backend as K
2
3
def soft_acc(y_true, y_pred):
4
return K.mean(K.equal(K.round(y_true), K.round(y_pred)))
5
6
model.compile( , metrics=[soft_acc])
7