gpt4 book ai didi

python - Keras:如何获得预测类的置信度?

转载 作者:太空宇宙 更新时间:2023-11-04 01:57:07 24 4
gpt4 key购买 nike

我在 Keras/TensorFlow 工作。这是我的 Keras 模型:

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

在训练步骤和测试步骤之后,我正在编写一个接受输入(我不知道他的类(class))的方法,这个方法返回具有置信度的类(class)预测。现在这个方法只返回类的预测。这是方法:

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"

我应该在方法中添加什么以获得相应预测的置信度?我不想使用变量“q”的置信度,但我想使用贝叶斯方法。我能怎么做?谢谢

最佳答案

Keras 中,model.predict() 实际上会返回置信度。因此,在代码片段中,您可能想要打印 q 以查看具有所有置信度的整个数组。

np.argmax(x) 为您提供数组中 X 具有最大值的参数(位置)。

关于python - Keras:如何获得预测类的置信度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56562623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com