[英]Keras: How to obtain confidence of prediction class?
I'm working in Keras/TensorFlow.我在 Keras/TensorFlow 工作。 This is my Keras model:
这是我的 Keras model:
model = Sequential()
model.add(Dense(512, input_shape=(max_words,)))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
After training step and test step, I'm coding a method that take the input (which i don't know his class) e this method returns the class prediction with level of confidence.在训练步骤和测试步骤之后,我正在编写一个采用输入的方法(我不知道他的类)e 这个方法返回 class 预测的置信度。 Now this method returns only the prediction of class.
现在这个方法只返回 class 的预测。 This is the method:
这是方法:
def predict(input):
try:
x_prediction = tokenize.texts_to_matrix(input)
q = model.predict(np.array([x_prediction[0],]))
predicted_label = text_labels[np.argmax(q)]
print("Prediction: " + predicted_label + "\n")
except:
return "Error"
What should I add in the method to get the confidence level of the respective prediction?我应该在方法中添加什么来获得相应预测的置信度? I don't want use the confidence of variable 'q' but I want to use the Bayes Approach.
我不想使用变量“q”的置信度,但我想使用贝叶斯方法。 How can I do?
我能怎么做? Thanks
谢谢
In Keras
, model.predict()
actually returns you the confidence(s).在
Keras
, model.predict()
实际上返回你的信心(或多个)。 So in the code snippet, you may want to print q
to see the entire array with all confidence levels.因此,在代码片段中,您可能希望打印
q
以查看具有所有置信度的整个数组。
np.argmax(x)
gives you the argument(position) in the array where X has the max value. np.argmax(x)
为您提供数组中的参数(位置),其中 X 具有最大值。
The values in the vector q
are probabilities for each class, which act as a confidence value, so you can just fetch the maximum value and return it as confidence.向量
q
中的值是每个类别的概率,它们充当置信度值,因此您可以获取最大值并将其作为置信度返回。
But notice that these probabilities are produced by the model, and they might be overconfident unless you use a model that produces calibrated probabilities (like a Bayesian Neural Network).但请注意,这些概率是由模型生成的,除非您使用生成校准概率的模型(如贝叶斯神经网络),否则它们可能会过度自信。
enter image description here在此处输入图像描述
In Keras, model.predict() actually returns you the confidence(s).在 Keras 中,model.predict() 实际上会返回您的置信度。 So in the code snippet, you may want to print q to see the entire array with all confidence levels.
因此,在代码片段中,您可能希望打印 q 以查看具有所有置信度的整个数组。
np.argmax(x) gives you the argument(position) in the array where X has the max value np.argmax(x) 为您提供数组中 X 具有最大值的参数(位置)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.