在将预测四舍五入到类之后,在 keras 中如何计算回归模型的准确性? [英] How do you compute accuracy in a regression model, after rounding predictions to classes, in keras?

查看:11
本文介绍了在将预测四舍五入到类之后,在 keras 中如何计算回归模型的准确性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何在 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?

虽然对于回归问题,准确度本身并没有按传统方式有效定义,但为了确定数据的有序类/标签,将问题视为回归是合适的.但是,同时计算准确度指标也会很方便,无论是 kappa 还是其他类似的指标.这是要修改的基本 keras 样板代码.

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)

推荐答案

我使用四舍五入的精度如下:

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

这篇关于在将预测四舍五入到类之后,在 keras 中如何计算回归模型的准确性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆