首页 > 解决方案 > 分类器如何预测一个类别?(对于张量流/keras)

问题描述

我想自己实现模型,因此必须知道它是如何对数据进行分类的。我为 12 类分类器建立了一个模型,它预测得很好。但是最后一个 conv 层只输出 12 个浮点值,我不知道它是如何突然预测正确的类的。

有人可以为我解释吗?就像它取决于某个阈值还是选择最大值或其他什么?谢谢!

标签: pythontensorflowkerasdeep-learningclassification

解决方案


您需要在模型中添加一个展平层,然后是一个密集层。密集层应该有 12 个节点并使用如下所示的 softmax 激活'您的模型现在将为每个图像输出 12 个概率值的列表。

flatten=tf.keras.layers.Flatten()(last_conv_layer)
output = Dense(12, activation='softmax')(flatten)
#after you train you can evaluate your model on your test set using model.evaluate() 
#to make Predictions use model.predict()
predictions=model.predict(.....
#you can get the index of the predicted class for the images you predict with
for p in predictions:
    predicted_index=argmax(p)
    print (predicted_index)

model.evaluate 和 model.predict 的文档在这里。添加两层后不要忘记重新编译模型。


推荐阅读