Keras CNN的SVC分类器以概率或置信度来区分未经训练的类别 [英] SVC Classifier to Keras CNN with probabilities or confidence to distinguish untrained classes

查看:205
本文介绍了Keras CNN的SVC分类器以概率或置信度来区分未经训练的类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与一个非常相似,并且基于GitHub上的帖子,将SVM多类别分类模型(例如,使用sklearn)转换为Keras模型.

This question is pretty similar to this one and based on this post over GitHub, in the sense that I am trying to convert an SVM multiclass classification model (e.g., using sklearn) to a Keras model.

具体来说,我正在寻找一种最终检索概率(类似于SVC probability = True )或置信度值的方法,以便我可以定义某种阈值并能够区分训练有素的班级和非训练有素的班级.那就是说,如果我用3或4个类训练模型,但使用未训练的5th模型,即使完全错误,它仍将输出一些预测.我想以某种方式避免这种情况.

Specifically, I am looking for a way of retrieving probabilities (similar to SVC probability=True) or confidence value at the end so that I can define some sort of threshold and be able to distinguish between trained classes and non-trained ones. That is if I train my model with 3 or 4 classes, but then use a 5th that it wasn't trained with, it will still output some prediction, even if totally wrong. I want to avoid that in some way.

我可以很好地完成以下工作,但是它依赖于最后选择最大值( argmax ),我想避免这种情况:

I got the following working reasonably well, but it relies on picking the maximum value at the end (argmax), which I would like to avoid:

  model = Sequential()
  model.add(Dense(30, input_shape=(30,), activation='relu', kernel_initializer='he_uniform'))
  # output classes
  model.add(Dense(3, kernel_regularizer=regularizers.l2(0.1)))
  # the activation is linear by default, which works; softmax makes the accuracy be stuck 33% if targeting 3 classes, or 25% if targeting 4.
  #model.add(Activation('softmax')) 
  model.compile(loss='categorical_hinge', optimizer=keras.optimizers.Adam(lr=1e-3), metrics=['accuracy'])

关于如何解决这个未经训练的班级问题的任何想法?如果我仍然可以将模型另存为onnx,则可以使用平台缩放"或温度缩放"之类的方法.

Any ideas on how to tackle this untrained-class problem? Something like Plat scaling or Temperature scaling would work, if I can still save the model as onnx.

推荐答案

我怀疑,通过缩放模型的功能(输入)使 softmax 可以正常工作.无需停止梯度或任何东西.我专门使用了很大的数字,尽管训练得很好,但它们阻止了 softmax (逻辑回归)正常工作.例如,可以通过以下代码完成功能的缩放:

As I suspected, got softmax to work by scaling the features (input) of the model. No need for stop gradient or anything. I was specifically using really big numbers, which despite training well, were preventing softmax (logistic regression) to work properly. The scaling of the features can be done, for instance, through the following code:

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_std = scaler.fit_transform(X)

通过执行此操作,使用keras的类似SVM的模型的输出将按原计划输出概率.

By doing this the output of the SVM-like model using keras is outputting probabilities as originally intended.

这篇关于Keras CNN的SVC分类器以概率或置信度来区分未经训练的类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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